help-octave
[Top][All Lists]

## Re: 3D array index as the element itself?

 From: Jordi Gutierrez Hermoso Subject: Re: 3D array index as the element itself? Date: Wed, 18 Apr 2007 16:52:51 -0500

```I love these vectorisation puzzles! :-)

On 18/04/07, Temp Orary <address@hidden> wrote:
```
```I'm a user using octave not on a daily basis but only sparcely. Something
came up to me that I believe is very easy to vectorize (if you are a
frequent user of octave) but it's not so obvious to me. So here it is:

for i=1:3
for j=1:3
for k=1:3
r(i,j,k, 1)=i;
r(i,j,k, 2)=j;
r(i,j,k, 3)=k;
endfor
endfor
endfor
```
```
The meshgrid available in Debian's octave2.9-forge package (which I
imagine must be the same meshgrid in the actual 'Forge pages) makes
this problem trivially simple to solve. The one in the octave2.1-forge
package doesn't seem to have this functionality for 3d arrays...

octave2.9:1> r = zeros(50,50,50); # Too large of an r, and all
# memory gets chewed up!

octave2.9:2> i = 1:size(r,1);
octave2.9:3> j = 1:size(r,2);
octave2.9:4> k = 1:size(r,3);

octave2.9:5> [iii,jjj,kkk] = meshgrid(i,j,k);

octave2.9:6> r(:,:,:,2) = iii; # Note how seemingly Octave
#forge's meshgrid flips i and j.
octave2.9:7> r(:,:,:,1) = jjj;
octave2.9:8> r(:,:,:,3) = kkk; # This variable name means
nothing.

octave2.9:9> r(3,42,5,2) ## Debugging...
ans =  42
octave2.9:10> r(3,42,5,1)
ans =  3
octave2.9:11> r(3,42,5,3)
ans =  5

I'm not quite sure why you would want to have a creature like r
running around... It seems to be too big to be easily managed and not
useful enough to justify its size.

HTH,
- Jordi G. H.

```