octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Attn: Patch Re: iterating cell arrays


From: John W. Eaton
Subject: Attn: Patch Re: iterating cell arrays
Date: Thu, 24 Jun 2004 22:21:39 -0500

On 25-Jun-2004, David Bateman <address@hidden> wrote:

| Ok, so if x is an NDArray or ND Cell array, Matlab iterates 
| prod(size(a)(2:end)), passing column vectors of length size(a,1). 
| This doesn't make any sense to me, since why is the first dimension
| privileged in this manner. To be consistent with the use of
| NDArrays elsewhere, the for loop should accept a "dim" argument that
| specifies the dimension over which the array is iterated. But that
| is likely to be very ugly. 

I think this feature is a holdover from the early days of Matlab, when

  for i = X
    ...
  end

assigned each column of X in turn to i for each trip through the
loop.  Since a colon expression like 1:N is really a matrix object
with one row, it is completely consistent to have

  for i = 1:N

assign 1, 2, ..., N to i in turn for each trip through the loop.

Once N-d arrays were introduced, I suppose it made some sense to
continue iterating over the all the "columns" of X, starting with the
first "page", etc.

It is probably simplest to implement this behavior if you first
reshape X using the equivalent of

  sz = size (X);
  X = reshape (X, sz(1), prod (sz(2:end)));

I think this will work in all cases, not just for N-d arrays.  You can
add a special case for row vectors for speed.  I think liboctave
already offers a way to extract columns from Matrix objects.  Perhaps
that functionality should be moved to the Array base class so that it
can also work for Cell objects.

jwe



reply via email to

[Prev in Thread] Current Thread [Next in Thread]