How to vectorize a find
이전 댓글 표시
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
Matlab2010
2013년 1월 15일
편집: Matlab2010
2013년 1월 15일
채택된 답변
추가 답변 (2개)
Sean de Wolski
2012년 12월 11일
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
2012년 12월 11일
편집: Mark Whirdy
2012년 12월 11일
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
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!