Can you remove an element from a multidimensal array?

Suppose if I had a 2 dimensional array:

a = Int32[1 2 3 4; 5 6 7 8; 9 10 11 12]

If I wanted to delete the first element in the second row, can I do that?

I attempted to search for myself and I found this Stack Overflow page, it talks about deleting a row rather than an element.

What does that mean? What resulting array do you want?

3 Likes

I’m guessing by your question that I can’t just remove an element, I would have to remove and replace an element, correct?

I guess my question should be can I remove 5 and replace it with another number?

A 2d array is a data structure that has the same number of elements in every row, so it’s not meaningful to “remove an element” — the result would no longer be an array,.

a[2,1] = 12345
7 Likes

Maybe this is helpful?

julia> a = Union{Int32,Missing}[1 2 3 4; 5 6 7 8; 9 10 11 12];

julia> a[2,1] = missing;

julia> a
3×4 Array{Union{Missing, Int32},2}:
 1          2   3   4
  missing   6   7   8
 9         10  11  12
1 Like

Or you could use some other kind of container, eg

a = Int32[1 2 3 4; 5 6 7 8; 9 10 11 12]
d = Dict([i => a[i] for i in CartesianIndices(a)])
delete!(d, CartesianIndex(2, 1))

It’s maybe worth mentioning that some languages (notably Javascript) use the word “array” to refer to what in Julia would be a Dict. A Julia Array is more akin to a Fortran array, which is a much simpler construction.

1 Like