Matrix Indices Problem
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Given a matrix e.g. A = [0 1 2 0 0; 0 3 4 5 0; 6 7 8 9 10; 0 0 0 11 0]
what MATLAB code will generate a vector of the column numbers of the first non-zero element in each row?
For this example the vector returned should be: [2;2;1;4]
0 Kommentare
Akzeptierte Antwort
Sean de Wolski
am 15 Aug. 2011
[junk, idx] = max(logical(A),[],2)
idx will be you index vector.
6 Kommentare
Fangjun Jiang
am 15 Aug. 2011
Use sort().
A = [0 1 2 0 0; 0 3 4 5 0; 6 7 8 9 10; 0 0 0 11 0];
[dummy,Ind]=sort(A~=0,2)
Ind=Ind(:,end)
To find first non-zero,
A = [0 1 2 0 0; 0 3 4 5 0; 6 7 8 9 10; 0 0 0 11 0];
[dummy,Ind]=sort(A~=0,2,'descend')
Ind=Ind(:,1)
Weitere Antworten (1)
Andrei Bobrov
am 15 Aug. 2011
size(A,2)+1-sum(cumsum(A,2)~=0,2)
more
(sum(cumsum(A')==0)+1)'
2 Kommentare
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!