octave-maintainers
[Top][All Lists]
Advanced

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

Re: indexing arrays and struct arrays with {}?


From: Jaroslav Hajek
Subject: Re: indexing arrays and struct arrays with {}?
Date: Sat, 6 Jun 2009 18:01:40 +0200

On Sat, Jun 6, 2009 at 4:19 PM, Thorsten Meyer<address@hidden> wrote:
> Hi,
>
> While working on the documentation of container types, I found that it is no
> longer possible to assign to a subarray of cell array c like this:
>  c{:,2} = 1

Yes. The correct way is c(:,2) = {1}. I think c(:,2) = 1 still works
as well. But c{:,2} was surely wrong, because it produces a cs-list
(and thus represents a sequence of lvalues rather than one).

> Instead, the {} operators consistently return a cslist of elements of a cell
> array. So, the {} operator now can be seen not to mean
>        "index a cell array"
> but
>        "generate a cs-list of elements (of a cell array)"

Or, equivalently, "extract elements".

> At the same time indexing with () means
>        "access a subarray"
> for arrays, cell arrays and struct arrays.
>

Yes, this is consistent. The difference is that singleton arrays are
auto-converted to scalars, but singleton cells are not.

> I think it could be quite useful to generalize the {} operator to arrays and
> structs as well, such that
>   a = [1, 2, 3; 4, 5, 6];
>   a{:,2}
> gave the cs-list
>   2, 5

It would not be hard. But I don't really see much use for this;
besides, it can be easily achieved using num2cell.

> And
>   b = struct("c", {1,"element2", "element3", 4});
>   b{[2,3]}
> gave the cs-list
>   b(1), b(2)

Here, I really can't think of a good use (maybe you have a good
example?). But anyway, again it would suffice if there was a function
converting the struct array to cell array of singleton structs.
Unfortunately, struct2cell does something different.
>From the point of view of efficiency, such a conversion is also highly
unfavorable - it will require much more memory than the struct array
form.

> Unfortunately, I do not have the c++ (or parser ?) skills to implement this.
> What do you think of such a change? How much work would it be? And can I maybe
> interest one of you octave core programmers in this (:-)?
>



-- 
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz



reply via email to

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