help-octave
[Top][All Lists]

## Re: 3D array

 From: Paul Kienzle Subject: Re: 3D array Date: Fri, 16 Nov 2001 10:37:12 -0500

```address@hidden wrote:

> On Tue, 13 Nov 2001, John W. Eaton wrote:
> > | I haven't used N dim arrays in Matlab. For those who have:
> > | Is that Matlab implementation well thought out?
> > | Or are there inconsistencies and ambiguities (as is
> > | fairly common in some of the worse thought out parts of
> > | Matlab syntax)?
> >
> > Well, it is apparently impossible to enter an N-d matrix directly
> > using the "[...]" syntax because of the historical interpretation of
> > things like
>
> >From this, and the other replys to my question, it seems
> that N dim arrays offer about the same functionality
> as lists of Matrices.  The only advantage being more
> convenient syntax.

There is better slicing with n-D arrays.   With a list of
matrices you are forced to use a for loop to get  all (x,z) for a
particular y.  For this purpose, you will be better off simulating
a 3-D array in Octave by concatenating matrices, e.g.,

M = [ M1; M2; ...; Mzn ];

Then an x-y slice at zi is :
M ( [1:xn]+(zi-1)*xn, : )
an x-z slice at yi is :
reshape ( M ( : , yi ), xn, zn)
and a y-z slice at xi is :
M ( xi+[0:zn-1]*xn, : )

What I have seen of matlab's nD array processing is that it is inconvenient
at best.  The linear algebra functions all expect matrices, so having grabbed
a slice you must collapse it to a matrix before operating on it.  Since it is
now
a matrix, you cannot plug the result of the slice back into your nD array until
you expand it again. For example, to multiply every y-z plane in M by the
matrix A you would need to do the following:

for i=1:size(M,1)
M(i,:,:) = reshape(A * squeeze(M(i,:,:)), [ 1 size(M,2) size(M,3)])
end

I just tried this in Matlab 6, and apparently you don't need to insert the
singular dimensions anymore, so it is just

for i=1:size(M,1)
M(i,:,:)  = A * squeeze(M(i,:,:))
end

Curiously,

A = [ 1 2 ; 3 4 ; 5 6]
A(:,2) = [1 2 3]         % this gives an error
A(:,2) = [1 2 3]'        % this doesn't
A(:,2,1) = [1 2 3]      % neither does this

I can see lots of merit in automatically turfing singular dimensions.   The
only time we care about row and column vectors is in distinguishing inner
and outer product, and the rest of the time the distinction just gets in the
way.  Furthermore, each time I use vector products I  have to check by
hand that I have the arguments in the right order because  it certainly
isn't intuitive from looking at the expression.  Using dot(x,y) for inner
product and * for outer product would suit me fine since it would make
it far more obvious what is being computed.  Can't do it though because
it would break a lot of stuff :-(

Tela defines products for arbitrarily shaped matrices, so long as the inner
dimensions agree.  For example you can take the product of a 2x3x4 matrix
and a 4x5 matrix yielding a 2x3x5 matrix.  I don't know how one would use
these sorts of things, or what other tensor operations one might want to
define.

Paul Kienzle

-------------------------------------------------------------
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
-------------------------------------------------------------

```