Thank you for the answer! I'm sorry about cross-posting. It won't happen 
again!

On Wednesday, December 10, 2014 11:08:53 PM UTC+2, Stefan Karpinski wrote:
>
> please don't cross-post questions in multiple places – it's the same 
> people reading and answering in both places: 
> http://stackoverflow.com/questions/27410076/multiple-assignment-in-multidimensional-array
>
> On Wed, Dec 10, 2014 at 4:06 PM, Stefan Karpinski <[email protected] 
> <javascript:>> wrote:
>
>> 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] 
>> <javascript:>> 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
>>>
>>>
>>
>

Reply via email to