Thread Subject: Column Index

Subject: Column Index

From: Oleg Komarov

Date: 1 Jul, 2009 20:42:01

Message: 1 of 4

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

Subject: Column Index

From: Jos

Date: 1 Jul, 2009 22:07:01

Message: 2 of 4

"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

Subject: Column Index

From: Bruno Luong

Date: 1 Jul, 2009 22:24:01

Message: 3 of 4

"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
>

Using SplitVec on FEX http://www.mathworks.com/matlabcentral/fileexchange/24255

IDX = NaN(size(a,1),1);

[J I]=find(a.');
last=SplitVec([I J],1,'last'); % On FEX
IDX(I(last)) = J(last)

% Bruno

Subject: Column Index

From: Oleg Komarov

Date: 1 Jul, 2009 22:46:01

Message: 4 of 4

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h2gnm1$o91$1@fred.mathworks.com>...
> "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
> >
>
> Using SplitVec on FEX http://www.mathworks.com/matlabcentral/fileexchange/24255
>
> IDX = NaN(size(a,1),1);
>
> [J I]=find(a.');
> last=SplitVec([I J],1,'last'); % On FEX
> IDX(I(last)) = J(last)
>
> % Bruno
Thank you very much Jos and Bruno

TO: Jos
I work with matrices which, by construction, in every row have at least one "1".

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
logical indexing Oleg Komarov 1 Jul, 2009 16:44:02
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com