[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Unidentified subject!
From: |
Paul Kienzle |
Subject: |
Re: Unidentified subject! |
Date: |
Sat, 27 Mar 2004 10:01:36 -0500 |
On Mar 27, 2004, at 8:58 AM, Antonio L. wrote:
Hello, mi problem can be split into two separated topics:
- Is there a more efficient way of doing this? (that is, without 'for'
looping):
for i=1:n
M(I(n,1),J(n,1)) = V(n,1);
endfor;
Where V,I,J are nx1 vectors, and M is a nxm matrix? Suposing indexing
M with
values contained in vectors I and J is always possible.
It should be obvious that the best way for doing so would simply be
M(I,J) = V;
but octave doesn't works like that.
M(I+(J-1)*rows(M)) = V;
or maybe
M(sub2ind(size(M),I,J)) = V;
- Supposing we can do that, what about dealing with the case where
[I,J] values
could index cells outside the given matrix? Just like the code below:
for i=1:n
for j=1:m
if ( (0<i<=rows(M)) && (0<j<=columns(M)) )
M(i,j) = M2(I(i,j),J(i,j));
else
M(i,j) = value;
endfor;
endfor
Where M, I, J are nxm matrices, and M2 is a general matrix. Is there an
efficient and elegant solution, preferredly avoiding 'for' loops?
M = value*ones(m,n);
valid= (0<i<=rows(M2)) & (0<j<=columns(M2))
M(I(valid)+(rows(M)-1)*J(valid))=M2(I(valid)+(rows(M2)-1)*J(valid));
Paul Kienzle
address@hidden
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------