Path: news.mathworks.com!not-for-mail 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$1@fred.mathworks.com> References: <ib0jl3$6es$1@fred.mathworks.com> Reply-To: <HIDDEN> NNTP-Posting-Host: webapp-02-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: fred.mathworks.com 1288951984 25900 172.30.248.37 (5 Nov 2010 10:13:04 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Fri, 5 Nov 2010 10:13:04 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 2158312 Xref: news.mathworks.com comp.soft-sys.matlab:684108 "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ib0jl3$6es$1@fred.mathworks.com>... > >> 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.