The classic vectorized Matlab approach is to use sub2ind:
julia> X[sub2ind(size(X), ind[:,1], ind[:,2])] = 2
2
julia> X
4x4 Array{Float64,2}:
2.0 0.0 0.0 0.0
0.0 2.0 0.0 0.0
0.0 0.0 2.0 0.0
0.0 0.0 0.0 0.0
Of course, in Julia you also have the option of just using a for loop:
for i = 1:size(ind,1)
X[ind[i,1],ind[i,2]] = 2
end
This is more efficient and arguably clearer.
On Wed, Dec 10, 2014 at 3:45 PM, Tudor Berariu <[email protected]>
wrote:
> Hello!
>
> I have a 4x4 array of zeros.
>
> julia> X = zeros(4,4)
> 4x4 Array{Float64,2}:
> 0.0 0.0 0.0 0.0
> 0.0 0.0 0.0 0.0
> 0.0 0.0 0.0 0.0
> 0.0 0.0 0.0 0.0
>
> I have an 2xN array containing indices of elements in X that I want to
> assign a new value.
>
> julia> ind = [1 1; 2 2; 3 3]
> 3x2 Array{Int64,2}:
> 1 1
> 2 2
> 3 3
>
> What is the simplest way to assign a value to all elements in X whose
> indices are rows in ind? (something like X[ind] = 2.0).
>
> julia> X
> 2.0 0.0 0.0 0.0
> 0.0 2.0 0.0 0.0
> 0.0 0.0 2.0 0.0
> 0.0 0.0 0.0 0.0
>
>