The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: sorting arrays in a cell array
Replies: 23   Last Post: Nov 15, 2012 11:16 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Steven Lord

Posts: 18,038
Registered: 12/7/04
Re: sorting arrays in a cell array
Posted: Nov 15, 2012 10:06 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"Bruce Elliott" <> wrote in message
> dpb <> wrote in message <k816f0$be5$>...
>> On 11/14/2012 4:21 PM, Steven_Lord wrote:
>> > c{2, 3}(1, 1)
>> Well, I'll be... :)
>> I'd never found that and I guess I never would've. There's no subsection
>> in the documentation specifically on the nomenclature and when I'd tried
>> before I'd never found the (apparently only) one line wherein it is
>> actually mentioned...I hadn't caught onto the meaning of the error being
>> that the curlies had to be first (and my old eyes are to the point I have
>> a heckuva' time distinguishing them from plain old paren's anymore...
>> :( ) but had always (for like 10+ yrs) presumed it just didn't like the
>> second set of addressing tacked on since never could find "multi-level
>> indexing" in a search...

Yes, increasing the discoverability of the full power of MATLAB is a
difficult problem. I've heard from some people that give training classes or
lectures that sometimes they'll show a feature and no one in the class knows
about it. Not so bad if the feature came out in the most recent release ...
bad when it was introduced five years ago. [Publishing is one feature that's
often slipped through the cracks.] There isn't really a good answer as far
as I'm aware, but we're trying.

>> So, OK, I stand corrected and I could've used cells much more efficiently
>> than have in the past having finally gotten that can.

> Ah! Okay, now I understand some of your previous statement more clearly.
> Yes the curly braces can take a while to master - I run into that all the
> time with new Matlab users.
> This: c(2,3){1,1} would imply that c is a "normal" array (all one type)
> because of the straight parens, but that the (2,3) element of that array
> contains a cell array -- because of the {1,1} -- which is not allowed.
> You parse this: c{2,3}(1,1) as, "c is a cell array (because it's indexed
> via curly braces); c{2,3} is the content of the cell with indices {2,3};
> c{2,3}(1,1) is the first element of the array contained in the cell at
> c{2,3}.
> The most confusing thing is that both c(2,3) and c{2,3} are allowed, but
> they are not the same. c(2,3) is a cell (always), but c{2,3} is the
> contents of that cell, so its type depends on what's stored there.

In almost all cases, y = x(indices) is of the same type as x itself. [For
function handles, x(indices) actually evaluates the function handle and so y
is of the type that function returns. Object authors can do whatever makes
sense for their objects and so this guideline may not apply to all objects.]

Curly brace indexing is only defined for cell arrays (and objects where the
authors have chosen to implement it) and the result of curly brace indexing
is of whatever type is stored INSIDE the cells.

> That's why you get (running your example myself, so different rand
> output):

>>> c(2,3) = {rand(4)};
>>> c(2,3)

> ans = [4x4 double]

>>> c{2,3}
> ans =
> 0.8147 0.6324 0.9575 0.9572
> 0.9058 0.0975 0.9649 0.4854
> 0.1270 0.2785 0.1576 0.8003
> 0.9134 0.5469 0.9706 0.1419

Yes. If you wrote:

clear c d
c(2, 3) = {rand(4)};
d{2, 3} = rand(4);
y = c(2, 3);
z = d{2, 3};

you should see in the output of "whos c d y z" that c and d are cell arrays,
y is a cell array, and z is a double array.

>> But I _still_ think not being able to general the cell references is a
>> mistake.

> By that, I take it you mean the "{:}" construct, e.g. C{:}(1,1). (?)
> I think that's the only thing missing.

I'm not sure if there are other considerations preventing that from working;
the example I gave was an off-the-top-of-my-head issue. If you feel strongly
that this construct should be supported in MATLAB, please let Technical
Support know and let them know how you would use it if it existed. The more
actual user use cases the developers have when designing a piece of
functionality, the higher the likelihood that it'll actually do what the
users want when or if it's implemented.

Steve Lord
To contact Technical Support use the Contact Us link on

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.