Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Column Index
Date: Wed, 1 Jul 2009 22:07:01 +0000 (UTC)
Organization: Erasmus MC
Lines: 23
Message-ID: <h2gmm5$kor$1@fred.mathworks.com>
References: <h2ghmp$t3a$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1246486021 21275 172.30.248.37 (1 Jul 2009 22:07:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 1 Jul 2009 22:07:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 870065
Xref: news.mathworks.com comp.soft-sys.matlab:552218


"Oleg Komarov" <oleg.komarov@hotmail.it> wrote in message <h2ghmp$t3a$1@fred.mathworks.com>...
> I have a matrix like this:
> a = random('bino',1,0.5,[10,5]);
> 
> And i want to find the colum index corresponding to the last 1 of each row:
> IDX = NaN(size(a,1),1);
> for i = 1:size(a,1)
>     IDX(i) = find(a(i,:),1,'last');
> end
> 
> Can it be done without the loop?
> 
> Thanks a lot
> 
> Oleg

One of the many approaches:

[i,idx] = max(cumsum(a,2),[],2)

but what if a row has no ones?

Jos