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: logical index in multi-dimensional arrary
Replies: 1   Last Post: Sep 16, 2013 4:37 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Loren Shure

Posts: 1,067
Registered: 12/7/04
Re: logical index in multi-dimensional arrary
Posted: Sep 16, 2013 4:37 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"jay cao" <> wrote in message
> Hi all,
> The following doesn't work. Why is it?
> test = ones(2, 3, 2)
> idx = [1, 0, 1; 0, 1, 0]
> idx = logical(idx)
> test(idx, 1)
> the error from the last command is "Index exceeds matrix dimensions." It
> works if test is a 2-d array, i.e., the following works.
> test = ones(2, 3)
> idx = [1, 0, 1; 0, 1, 0]
> idx = logical(idx)
> test(idx)
> why doesn't the logical index work in high dimensional array as in 2-d
> array? How can I get around with it, say i want to assign some values to
> test(idx, 1) where idx is a logical index 2-2 array.
> Thanks.
> Jay

test holds 12 values in a 2x3x2 array. idx holds 6 values.

When you index into test as test(idx,1) you are saying you want 6 row values
and are combining columns and pages in the 2nd index. To MATLAB, it is
trying to index into test as if test's size was 6x prod(3*2). But really,
as a 2-d view, test's size is 2by 6 -- not 6x6.

MATLAB will only collapse trailing dimensions for the purposes of indexing,
not the leading ones. You can, however, collapse to a single dimension -
which is why your 2-d case works.


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.