[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: Arrays: Multidimensional manipulating
From: 
William Krekeler 
Subject: 
RE: Arrays: Multidimensional manipulating 
Date: 
Tue, 12 Apr 2011 19:26:52 +0000 
Original Message
From: John W. Eaton [mailto:address@hidden
Sent: Tuesday, April 12, 2011 11:51 AM
To: William Krekeler
Cc: dirac; address@hidden
Subject: RE: Arrays: Multidimensional manipulating
On 12Apr2011, William Krekeler wrote:
 Maybe you could try using meshgrid.

 [A,B,K] = meshgrid(a,b,k);
 p = size(A);
 p = exp(1.*A.*(abs(K)).^B);
What is the second line for?
Using meshgrid is good if you need exactly three dimensions. If you
need more, then you will need something like the method I showed with
repmat and reshape. But the basic idea is the same. You need to
generate a set of matrices all with the same size that together
contain all the combinations that you are looking for. Of course if
you need many dimensions, you will quickly run out of memory storing
the temporary matrices that represent all combinations...
jwe
I meant to type 'p = zeros( size(A) );' to preallocate the array. Either way it
is an unnecessary step.
I agree that using meshgrid will result in increased memory usage, but for the
small matrices used I think (untested) that the meshgrid vector math solution
may be faster than a sequence of for loops. Vector computations is generally
faster than for loops, at least in base Octave without compilation. As Octave
does not benefit from for loop acceleration like Matlab's locally compiled just
in time for loop implementation.
William Krekeler