How to implement something similar to Fancy indexing in Julia?

• A+
Category：Languages

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

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).

``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 ``