help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: vectorization


From: Ben Abbott
Subject: Re: vectorization
Date: Sun, 11 Jan 2015 13:59:55 -0500

> On Jan 11, 2015, at 12:13 PM, Dave Cottingham 2 <address@hidden> wrote:
> 
> Nidjara wrote
>> Hi,
>> 
>> I have 3 vectors (nx1) of real numbers, denote them with L1, L2, L3, and
>> (nxn) matrix G. I'm trying to vectorize the following loop:
>> 
>>  for i=1:n
>>    G(L1(i), L2(i))=L3(i);
>>  end
>> 
>> but this appears not to be as simple as it seems. Can you please help.
>> 
>> Thanks!
> 
> One solution is to use the fact that you can index an array, which normally
> would have two indices,  with a single index. So that would be something
> like
> 
> G((L1 - 1) * n + L2) = L3;
> 
> I may have L1 and L2 mixed up there; a little experimenting should
> straighten it out.

Or use sub2ind () ...

G(sub2ind (size (G), L1, L2)) = L3;

Ben





reply via email to

[Prev in Thread] Current Thread [Next in Thread]