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

**URL:** <https://discourse.julialang.org/t/how-can-i-update-matrix-value-by-using-row-and-column-vector-julia/27779>\
**Category:** Specific Domains\
**Tags:** linearalgebra\
**Created:** [August 21, 2019, 2:21am UTC](https://discourse.julialang.org/t/how-can-i-update-matrix-value-by-using-row-and-column-vector-julia/27779 "2019-08-21T02:21:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manu\_Francis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_francis/32/10010_2.png) [@Manu\_Francis](https://discourse.julialang.org/u/Manu_Francis)\
**Post date:** [August 21, 2019, 2:21am UTC](https://discourse.julialang.org/t/how-can-i-update-matrix-value-by-using-row-and-column-vector-julia/27779/1 "2019-08-21T02:21:12Z")

</div>

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:

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

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [August 21, 2019, 2:27am UTC](https://discourse.julialang.org/t/how-can-i-update-matrix-value-by-using-row-and-column-vector-julia/27779/2 "2019-08-21T02:27:25Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Manu\_Francis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_francis/32/10010_2.png) [@Manu\_Francis](https://discourse.julialang.org/u/Manu_Francis)\
**Post date:** [August 21, 2019, 3:01am UTC](https://discourse.julialang.org/t/how-can-i-update-matrix-value-by-using-row-and-column-vector-julia/27779/3 "2019-08-21T03:01:10Z")

</div>

@mohamed82008 Thanks a lot…it works well.

Thanks and Regards,  
Manu
