[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: iterating cell arrays
From: |
David Bateman |
Subject: |
Re: iterating cell arrays |
Date: |
Fri, 25 Jun 2004 10:26:11 +0200 |
User-agent: |
Mutt/1.4.1i |
According to John W. Eaton <address@hidden> (on 06/25/04):
> 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)));
This won't work since you have to iterate over the first non-singleton
dimension of the array. Thus the above code wouldn't work for the
case "a=rand(1,2,2);for i=a; disp(i); end;"
Basically the patch I submit in the previous mail implements
sz = size(X);
dim = 1;
for i=1:ndims(X)
if (sz(i) > 1)
dim = i;
break;
endif
endfor
X = reshape (X, sz(dim), prod(sz((dim+1):end)));
Before iterating over the array. In fact it uses an idx_vector and the
ArrayN<T>::index function to extract parts of the array in the loop. It
works with all array objects including row and column vectors.
>
> 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.
Effectively the code needed is already in Array<T>::index..
Cheers
David
--
David Bateman address@hidden
Motorola CRM +33 1 69 35 48 04 (Ph)
Parc Les Algorithmes, Commune de St Aubin +33 1 69 35 77 01 (Fax)
91193 Gif-Sur-Yvette FRANCE
The information contained in this communication has been classified as:
[x] General Business Information
[ ] Motorola Internal Use Only
[ ] Motorola Confidential Proprietary