From help-octave-request at bevo dot che dot wisc dot edu Sun Dec 28 15:44:23 2003 Subject: Re: Effectively selecting elements of a matrix From: "Jonathan C. Webster" To: Thomas Weber CC: help-octave at bevo dot che dot wisc dot edu Date: Sun, 28 Dec 2003 16:33:21 -0500 Thomas Weber wrote: > 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: > > Ravel the matrix A out into one long vector, A_vec, using fortran indexing if you want. Then combine vec1 and vec2 into a single index vector into your A_vec. . vec3 = vec1 + size(A)(1) * (vec2 - 1) Finally, your new reordered vector A3_vec A3_vec = A_vec(vec3) I think thats right, but check it. Jonathan ------------------------------------------------------------- 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 -------------------------------------------------------------