[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
extracting elements without 'for' loop
From: |
Mike Miller |
Subject: |
extracting elements without 'for' loop |
Date: |
Sun, 6 Jul 2003 19:54:14 -0500 (CDT) |
I have an n-by-m matrix W that tells me which m elements to extract from
each of the n rows of the n-by-2m matrix G. So, if these were G and W:
G=reshape([1:24],4,6); W=[1 3 6;2 3 5;1 4 5 ; 2 4 6];
G =
1 5 9 13 17 21
2 6 10 14 18 22
3 7 11 15 19 23
4 8 12 16 20 24
W =
1 3 6
2 3 5
1 4 5
2 4 6
I would want to extract this matrix from G:
G(1,W(1,1)) G(1,W(1,2)) G(1,W(1,3))
G(2,W(2,1)) G(2,W(2,2)) G(2,W(2,3))
G(3,W(3,1)) G(3,W(3,2)) G(3,W(3,3))
G(4,W(4,1)) G(4,W(4,2)) G(4,W(4,3))
Both G and W will sometimes be fairly large. I want to avoid 'for' loops
(I assume they'll slow me down). This seems to work...
V=reshape(G(:,W),n^2,size(W,2));
output=V([1:n+1:n^2],:);
...but the matrix V will be very large with large n. This was another of
my ideas...
V=reshape([G(:,W');zeros(1,m*n)]',(n+1)*m,n)';
output=V(1:n,1:m);
...which seems to give the same answer, but it has the same problem of
creating a large matrix V that is only discarded.
Any thoughts?
Mike
-------------------------------------------------------------
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
-------------------------------------------------------------
- extracting elements without 'for' loop,
Mike Miller <=