From help-octave-request at bevo dot che dot wisc dot edu Sun Dec 28 14:38:45 2003 Subject: Effectively selecting elements of a matrix From: Thomas Weber To: help-octave at bevo dot che dot wisc dot edu Date: Sun, 28 Dec 2003 21:38:14 +0100 Hello, my problem is as follows: I have a matrix A (size: 50x500) and two vectors (vec1, vec2) of length 32000. The elements of the vectors are indices for the matrix, i.e. if vec1(4) = 5 and vec2(4) = 10, I want to select A(5,10) into a new vector results_vec at position 4: results_vec (4) = A(5,10) My problem is how to create this new vector effectively: I tried something like results_vec = diag(A(vec1, vec2)); which works only for smaller (shorter) vectors vec1 and vec2. Octave needs to create a new matrix of size 32000x32000 which results in a out-of-memory error. I am currently using a for-loop result_vec = zeros(1,32000); for k=1:32000 result_vec(k) = A(vec1(k), vec2(k)); endfor; but this is really slow. I am grateful for any ideas. Best regards Thomas ------------------------------------------------------------- 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 -------------------------------------------------------------