help-octave
[Top][All Lists]

## Re: confused about how to define a cell array of vectors

 From: DushanM Subject: Re: confused about how to define a cell array of vectors Date: Thu, 15 Nov 2007 18:35:13 -0700 User-agent: MacSOUP/2.8.1 (Mac OS X version 10.4.10 (x86))

```James Sherman Jr. <address@hidden> wrote:

> So, I think this is what you're looking for:
> Let X, Y and Z be size m,n.  Then make the matrix W, such that
> W(1:m:3*m,:) = X;
> W(2:m:3*m,:) = Y;
> W(3:m:3*m,:) = Z;
> Then you can use mat2cell:
> CA = mat2cell(W, 3*ones(1,m), ones(1,n));
>
> I think this is what you're shooting for.

A big Thank You, James, this looks just like what I was looking for.
Now to try it out...

... several hours later...

Yep, it works like a charm, after I finally found a typo bug I'd been
blind to.

Another arrow in my Octave quiver.  Again, thanks.

- Dushan

> On Nov 15, 2007 3:40 AM, DushanM <address@hidden> wrote:
> > I've still not puzzled this out, and my earlier question about it didn't
> > get a response, so I'm trying again.  Maybe rephrasing will help.
> >
> > I have a 2-D, curved grid of points, indexed by (m,n).  The (x,y,z) com-
> > ponents of each point can be computed, giving three 2-arrays X, Y, and Z
> > so that X(m,n) is the x-component of the (m,n) point, etc.  What I would
> > now like to do, but don't know how, is define a 2-D cell array CA with
> > each cell a 3-vector: CA{m,n} = [ X(m,n); Y(m,n); Z(m,n) ].
> >
> > Could someone please point me to how to do this, or to where I can find
> > out?  Thanks.
>
> > James Sherman Jr. <address@hidden> wrote:
> > > I'm a bit confused, and maybe others are too.  Why can't you simply
> > > write a for loop like:
> > >
> > > CA = cell(size(X));
> > > for m = 1:size(X,1),
> > >  for n = 1:size(X,2),
> > >   CA{m,n} = [X(m,n), Y(m,n), Z(m,n)];
> > >  end
> > > end
> > >
> > > Is there something I'm missing?
> > >
> > > DushanM wrote:
> > > > Sorry, I forgot to mention this.  Yes, the 'for' loops are an obvious
> > > > way to do it.  What I was wondering is if there is a straightforward
> > > > vectorized way to do this.  If the index limits are modest (and I'm not
> > > > sure what upper value 'modest' may have in this context) the time cost
> > > > of the 'for' loops is unimportant, but for large limits it could become
> > > > significant.  In that case, is there a way of avoiding the 'for' loops?
> > > >
> > > > - Dushan

```