How can I update matrix value by using row and column vector julia

Hello,

I have an initialized 2D Float array as follows:
proj_img = zeros(1024,1024)

I have another 2D Float array contain a specific row and column location as follows:

pix_vis_locs=  [621  1
                622  2
                623  3
                624  4
                625  5
                ]

The first column is corresponding to row location and second column corresponding to the column location of first matrix proj_img. I am trying to update the values of specified indices of proj_img as follows similar to MATLAB:

proj_img[pix_vis_locs[:,1],pix_vis_locs[:,2]] .= 1

I expected sum(proj_img)==5 but I got 25. Then I realized that this method updates all combination of row and columns.

But I need to update values only in above mentioned 5 indices. How can I do that?

Thanks in Advance !!
Manu

There are multiple ways to do this, here is one:

julia> A = rand(3,3); A[CartesianIndex.(1:3, 1:3)] .= 1;

julia> A
3×3 Array{Float64,2}:
 1.0       0.874314  0.485028
 0.369039  1.0       0.24893
 0.903562  0.729538  1.0

Replace the 1:3 by your index vectors. A loop also works fine and is fast.

2 Likes

@mohamed82008 Thanks a lot…it works well.

Thanks and Regards,
Manu