- A+

I am very to new to Julia. Therefore, please excuse me if this is a silly problem.

I have a 4*4 Matrix `AA`

.

`julia> AA 4×4 Array{Int64,2}: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 `

I have an array `pv`

and `pq`

containing indexing information.

`julia> pv 3-element Array{Int64,1}: 1 3 4 julia> pq 3-element Array{Int64,1}: 2 1 4 `

I wish to assign zero to the elements `AA[1,2]`

, `AA[3,1]`

and `AA[4,4]`

.

If I run `AA[pv,pq] = 0`

then output is:

`4×4 Array{Int64,2}: 0 0 3 0 5 6 7 8 0 0 11 0 0 0 15 0 `

But I need this as output

`4×4 Array{Int64,2}: 1 0 3 4 5 6 7 8 0 10 11 12 13 14 15 0 `

I know this is called Fancy Indexing in Python. But, how can I do this in Julia? I cannot run two if-statements with variables which checking row and column index.

Thanks

Adding my 2¢ for completion,

Julia supports 'column-major-order' linear indices like matlab (although I note that Julia 1.0 has deprecated the `sub2ind`

and `ind2sub`

matlab-equivalent functions).

Therefore, if you could express (or already had) your coordinates in "linear indices" form, you could use those directly for your indexing.

In practice, in the current state of julia, this does not look much different to using a Cartesian Index, but it may be preferred in some cases, e.g. if you know you will be reusing those indices for further operations, or for readability / shifting from matlab code etc.

E.g.

`julia> A = copy( reshape( 10:10:160, (4,4) )' ) 4×4 Array{Int64,2}: 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 julia> pv = [1,3,4]; pq = [2,1,4]; julia> A_linearIndices = LinearIndices(A) 4×4 LinearIndices{2,Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}}: 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16 julia> L = [ A_linearIndices[i...] for i in zip(pv, pq) ] 3-element Array{Int64,1}: 5 3 16 julia> A[L] 3-element Array{Int64,1}: 20 90 160 julia> A[L] .= 0; A 4×4 Array{Int64,2}: 10 0 30 40 50 60 70 80 0 100 110 120 130 140 150 0 `

Having said that, if you wanted to keep something for later indexing, you could also be saving a Boolean matrix for logical indexing via the same approach.

_{EDIT: Code edited to show assignment via linear index too. Note that I had to make A a proper array, rather than keep as a "ReshapedArray" object, as the latter complains if you try to index it directly.}