[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: 'length' extension for multiple input arguments?
From: |
Søren Hauberg |
Subject: |
Re: 'length' extension for multiple input arguments? |
Date: |
Wed, 04 Feb 2009 22:33:45 +0100 |
ons, 04 02 2009 kl. 22:29 +0100, skrev Thomas Treichl:
> Soren Hauberg schrieb:
> > Hi All
> >
> > If I have a cell array, and I want to know the length of all elements of
> > this array, it seems I, essentially, have to do something like
> >
> > for k = 1:length (cell_array)
> > lengths (k) = length (cell_array {k});
> > endfor
> >
> > Would an extension to the 'length' function be acceptable that allowed
> > for multiple input arguments? This would simply implement the above loop
> > and return a vector of integers instead of just an integer. So,
> >
> > length ("hello", 7)
> >
> > would return [5, 1]. Such an extension would allow me to write
> >
> > lengths = length (cell_array {:});
> >
> > which I think is quite easy to read. Anyway, would such a change be
> > acceptable?
>
> You could also use cellfun instead, if I understand you correctly.
> But this is just a tip making things faster and doesn't really answer
> your question if a modification to length() can be made...
>
> octave-3.1.51+:1> A = {"hello", 7, "hello world"};
> octave-3.1.51+:2> B = cellfun (@(x) length (x), A, "UniformOutput", true)
> B =
> 5 1 11
> octave-3.1.51+:3> typeinfo (B)
> ans = matrix
Yeah, I guess I should mentioned this in my previous mail... My problem
with the above solution is that I find the code hard to read. It can be
made somewhat simpler just by doing
cellfun ("length", {"hello", 7, "hello world"})
as 'cellfun' has a special case for the 'length' function. But I still
find calling 'length' directly much more intuitive.
Soren
P.S. I do realise that this is really a _minor_ issue, but I've been
annoyed with it several times.