help-octave
[Top][All Lists]

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

 From: John W. Eaton Subject: Re: 3D array index as the element itself? Date: Wed, 18 Apr 2007 18:09:19 -0400

```On 18-Apr-2007, Jordi Gutierrez Hermoso wrote:

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

I'm not sure that this is what the OP really wants, but this works in
2.9.10:

tmp(1,1,1,:) = 1:3;
r = repmat (tmp, [3,3,3])

you can use any dimensions, not just [3,3,3] here.

Do you realize that 512x512x512x3 requires 3GB of memory?  Octave
won't be able to handle an array of that size on a 32-bit system.
Also, since it is just a lot of repeated values, it seems somewhat
pointless to store it.

jwe

```