Search code examples
arraysindexingjulia

Indexing in Julia


I want to set (1,1),(2,1),(3,3) element of matrix A to be 1.

In python I can do something like.

A=zeros(Int,4,4)
A[[1,2,3],[1,1,3]]=1

This does not work. What is the equivalent in Julia?


Solution

  • The following works (although a for loop likely will be more efficient). A[CartesianIndex.([(1,1), (2,1), (3,3)])] .= 1