Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Find first value in each row

4 views
Skip to first unread message

foof...@gmail.com

unread,
May 13, 2009, 3:22:09 AM5/13/09
to
How can I find the first index with a value in each row without using
a for loop?

K>> mat=magic(4)>5

mat =

1 0 0 1
0 1 1 1
1 1 1 1
0 0 1 0

K>>

Here I would like to obtain 1 for the first row, 2 for the second row,
1 for the third row, and 3 for the fourth row.

Yi Cao

unread,
May 13, 2009, 3:39:01 AM5/13/09
to
foof...@gmail.com wrote in message <e30b45b3-83c9-44d9...@q2g2000vbr.googlegroups.com>...

[dum,idx]=sort(mat,2,'descend');
firstOne=idx(:,1)

HTH
Yi Cao

Nasser Abbasi

unread,
May 13, 2009, 3:46:25 AM5/13/09
to

<foof...@gmail.com> wrote in message
news:e30b45b3-83c9-44d9...@q2g2000vbr.googlegroups.com...

Yi Cao gave a nice solution.
This is another using arrayfun

mat=magic(4)>5

arrayfun(@(i) find(mat(i,:)~=0,1,'first'),1:size(mat,2))

ans =

1 2 1 2

--Nasser


Nasser Abbasi

unread,
May 13, 2009, 3:52:14 AM5/13/09
to

<foof...@gmail.com> wrote in message
news:e30b45b3-83c9-44d9...@q2g2000vbr.googlegroups.com...
> How can I find the first index with a value in each row without using
> a for loop?
>
> K>> mat=magic(4)>5
>
> mat =
>
> 1 0 0 1
> 0 1 1 1
> 1 1 1 1
> 0 0 1 0
>

btw, your mat from your magic(4)>5 command is different than what I get.
Your 4th row does not seem to be correct

EDU>> mat=magic(4)>5

mat =

1 0 0 1
0 1 1 1
1 1 1 1

0 1 1 0

EDU>> ver
-------------------------------------------------------------------------------------
MATLAB Version 7.7.0.471 (R2008b)

Which Matlab version did you use? what is your output for magic(4) ?

--Nasser


foof...@gmail.com

unread,
May 13, 2009, 4:14:44 AM5/13/09
to
On May 13, 9:52 am, "Nasser Abbasi" <n...@12000.org> wrote:
> <foofig...@gmail.com> wrote in message

Sorry about that. I modified 'mat' by using mat(ind,ind) = 1 so that
it is not a result from magic(4)..

Thank you all for your help.

Jos

unread,
May 13, 2009, 4:50:03 AM5/13/09
to
"Nasser Abbasi" <n...@12000.org> wrote in message <HUuOl.16153$hc1....@flpi150.ffdc.sbc.com>...

And yet a few others:
mat = magic(4)>5 % data

[c1,dummy] = find(mat.'==1 & cumsum(mat.',1)==1) % fast

[c,r] = find(mat)
c2 = accumarray(c,r,[size(mat,1) 1],@min)

hth
Jos

Bruno Luong

unread,
May 13, 2009, 5:12:02 AM5/13/09
to
"Yi Cao" <y....@cranfield.ac.uk> wrote in message <gudtel$ej3$1...@fred.mathworks.com>...

>
> [dum,idx]=sort(mat,2,'descend');
> firstOne=idx(:,1)

Beware of the result when a row is all-zero.

Bruno

Bruno Luong

unread,
May 13, 2009, 5:16:01 AM5/13/09
to
Another solution:

[m where]=max(mat,[],2)
where=where.*(m==1) % 0 for all-zero row

Bruno

Loren Shure

unread,
May 13, 2009, 8:00:44 AM5/13/09
to
In article <gue1jr$fdp$1...@fred.mathworks.com>, #10...@fileexchange.com
says...

This only works if all matrix values are real and non-negative.

--
Loren
http://blogs.mathworks.com/loren

Matt Fig

unread,
May 13, 2009, 9:25:03 AM5/13/09
to
Yet another approach.

% Data
mat = round(rand(4)*3)
N = 3; % The number to find.


COLS = zeros(size(mat,1),1); % Could use zeros() or nan().
TMP = mat==N;
[r,c] = find(TMP & cumsum(TMP,2)==1);
COLS(r) = c

Siyi

unread,
May 13, 2009, 11:29:57 PM5/13/09
to
Here's a way without using "find":


m = round(rand(9,4)*3)
[r,c] = size(m)
n = 3; % number to find.

idx = mod(sum(cumprod(double(m ~= n),2),2)+1,c+1)

Matt Fig

unread,
May 14, 2009, 12:54:01 AM5/14/09
to
Siyi <Mr.Siy...@gmail.com> wrote in message <9e6facbd-b12d-42ac...@u9g2000pre.googlegroups.com>...


I think that's pretty slick, Siyi. It does bring up a question: why does cumsum work on logicals but cumprod does not? I hadn't noticed that before.

Jos

unread,
May 14, 2009, 7:29:02 AM5/14/09
to
"Jos " <#10...@fileexchange.com> wrote in message <gue1jr$fdp$1...@fred.mathworks.com>...

and another route to Rome:

M = ceil(10*rand(10,8)) ;
val = 6 ;

[mx,idx] = max((M==val),[],2)
idx(mx==0) = 0

Jos

Bruno Luong

unread,
May 14, 2009, 7:40:03 AM5/14/09
to
"Jos " <#10...@fileexchange.com> wrote in message <gugv9u$env$1...@fred.mathworks.com>...

>
> and another route to Rome:
>
> M = ceil(10*rand(10,8)) ;
> val = 6 ;
>

Actually it's the same route as ... in post #8

Bruno

0 new messages