Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: re-positioning a Matrix
Date: Thu, 22 May 2008 11:14:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 39
Message-ID: <g13khq$6ae$1@fred.mathworks.com>
References: <g13865$nl6$1@fred.mathworks.com> <g139di$727$1@fred.mathworks.com> <g13b7p$t99$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1211454842 6478 172.30.248.35 (22 May 2008 11:14:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 22 May 2008 11:14:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 870065
Xref: news.mathworks.com comp.soft-sys.matlab:469837



"Emanuele " <emanuelemignosa.nospam@mathworks.com> wrote in
message <g13b7p$t99$1@fred.mathworks.com>...
< SNIP .. trouble understanding (logical) indexing

Hi Emanuele,

If you break down the code "M2(:,c(c>0)) = M(:,c>0)" into
single lines, you might be able to follow

1) q = c > 0
returns a logical array where c is larger than zero

2) COLIND = c(q)
returns those elements in c that are larger than zero

3) M2(:,colind) selects only those columns specified by
COLIND (called "normal" indexing)

4) M(:,q) selects those columns for which q is true (called
logical indexing)

You can use indexing on both the lefthand or righthand side
of an assignment operator. For instance

X(3) = 2 sets the third element of X to 2

j = X(5) retrieves the fifth element of X

If you use them on the lefthand side you need to be sure
that the number of elements match:

X = [0 0 0 0] ;
X(2) = [2 3] ;  error!

hth
Jos