From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: logical indexing with larger logical array
Date: Fri, 5 Nov 2010 10:13:04 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 26
Message-ID: <ib0lbg$p9c$>
References: <ib0jl3$6es$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1288951984 25900 (5 Nov 2010 10:13:04 GMT)
NNTP-Posting-Date: Fri, 5 Nov 2010 10:13:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2158312
Xref: comp.soft-sys.matlab:684108

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ib0jl3$6es$>...
> >> a = [1 2 3];
> >> a([false false true false false])
> ans =
>      3
> >> a([false false true true])
> ??? Index exceeds matrix dimensions.
> >> 
> Can someone shed a light (or point towards a document that explains) why the second command throw an error and not the first?
> Bruno 

MatLab is very flexible and laid back compared to most environments!

In the example a([false false true false false]), it doesn't matter that you are indexing with a matrix larger than a because you are not asking it to return any elements beyond a's dimensions.

However, in a([false false true true]), you are asking MatLab to return the 3rd and 4th elements of a. But a only has 3 elements. Here there is nothing MatLab can do but return an error.

If you are a programmer, you would probably expect both commands to return an error. It is to MatLab's credit that the first command works. This philosophy is evidenced elsewhere, such as the command x(20) = 1;
Then ML will create a double variable, make it 20 elements, populate them all with zeros and then the last one with 1. In most languages, you would have to do most of this in separate steps.