From help-octave-request at bevo dot che dot wisc dot edu Sun Dec 5 23:54:36 1999 Subject: Re: Dot product (nx1).*(nxm) From: Mike Miller To: "'help-octave at bevo dot che dot wisc dot edu'" Date: Sun, 5 Dec 1999 23:54:34 -0600 (CST) On Sun, 5 Dec 1999, Joao Cardoso wrote: > Mike Miller wrote: > > > octave:1> p=rand(100,1); > > octave:2> q=ones(1,50); > > octave:3> p2=p*q; > > octave:4> t0=cputime; for i=1:1000, p2=p*q; end , cputime-t0 > > ans = 2.4600 > > octave:5> t0=cputime; for i=1:1000, p2=p(:,q); end , cputime-t0 > > ans = 2.0100 > > As I never really understood the line 5 syntax, I always use the line > 4 method -- I understand it. Joao-- I think it's pretty simple to understand. If you want to select rows and colums from a matrix A, you can give a vector of rows and a vector of columns as follows: A([1 3],[2 4]) That will return the first and third rows of A, but only the 2nd and 4th columns of those rows. You can repeat rows or columns if you like: A(:,[2 2 2 4]) That will return the all rows of A (the colon ':' means 'all'), but it will repeat the second column three times followed by the fourth column. There will be no other columns. So, if we have a column vector 'p' and we write this: p(:,[1 1 1 1]) That yields a matrix containing only four copies of p in the four columns of the matrix. So, of course, p(:,ones(1,m)) does the same thing with m copies of vector p. Does that clarify the meaning of line 5 above? MATLAB allows users to use a single index vector where the elements of the vector correspond to individual elements of the matrix. For an mxn matrix, MATLAB numbers elements as follows: 1 m+1 2m+1 ... (n-1)m+1 2 m+2 . ... (n-1)m+2 3 m+3 . . . . . . . . . . m 2m 3m .... nm Then, A([3 7 11 81]) would return a row vector with elements given by elements 3, 7, 11 and 81 of matrix A, and A([3 7 11 81]') would return a column vector of the same elements. This functionality is not available in Octave (at least not in 2.0.14). But then we have the following useful syntax, A(:) which returns a column vector of all the elements of A ordered by their element numbers (as in the matrix above). The A(:) syntax *is* available in Octave. Is there a reason why we don't want the single index to be used in Octave? If not, it could be useful. For example, it is often used with the 'find' command. In both Octave and MATLAB, the following command returns the element numbers of elements of A that are less than .5: find(A<.5) So far so good, but in MATLAB you can get those elements out of the matrix and into a column vector using this command: A(find(A<.5)) That doesn't work in Octave. Is there some other way that Octave can extract those numbers from the matrix? Is it as efficient? Regards, Mike -- Michael B. Miller University of Missouri--Columbia http://taxa.psyc.missouri.edu/~mbmiller/ ----------------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.che.wisc.edu/octave/octave.html How to fund new projects: http://www.che.wisc.edu/octave/funding.html Subscription information: http://www.che.wisc.edu/octave/archive.html -----------------------------------------------------------------------