How to vectorize a find
Ältere Kommentare anzeigen
x = zeros(1000,1);
x(76) = 1;
x(100) = 1;
x(200) = 1;
I can do:
first = find(x ==1, 1, 'first'); %the answer =76
but this is slow. I would like to speed this up. How? thanks
1 Kommentar
Matlab2010
am 15 Jan. 2013
Bearbeitet: Matlab2010
am 15 Jan. 2013
Akzeptierte Antwort
Weitere Antworten (2)
Sean de Wolski
am 11 Dez. 2012
If you only have zeros and ones and you are positive there is atleast one one, then you can use the second output from max().
[~,first] = max(x);
I don't know if this will be faster or not.
Mark Whirdy
am 11 Dez. 2012
Bearbeitet: Mark Whirdy
am 11 Dez. 2012
a temporary vector of row numbers, then use your vector-of-interest & a logicsl statement to index this row-number vector
a =[1:size(x,1)]'; % row numbers
b=a(x==1); % logical indexing the populated rows
b(1) % first instance
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!