find index in a matrix without 'find' function

조회 수: 7 (최근 30일)
Naz khan
Naz khan 2021년 6월 12일
편집: Adam Danz 2021년 6월 14일
I have an array of 1000+ samples. I want to find the index of nonzero elements in a large matrix in a shortest possible time. Whats the best way to get the list of indexing without using "find" funcion?
Example
p=[0 0 .34 0; 3 0 0 3; 0 9 0 0];
v=nonzeros(p)
for i=1:length(v)
[r,t]=find(p==v(i))
sds{i}=[r,t];
end;

답변 (1개)

Adam Danz
Adam Danz 2021년 6월 12일
편집: Adam Danz 2021년 6월 14일
find() is the quickest way to return the subscript indices of non-zero elements of a matrix but it's quicker to do so without using a loop. Is there a reason you can't use find?
p=[0 0 .34 0; 3 0 0 3; 0 9 0 0];
[rows, cols] = find(p~=0)
rows = 4×1
2 3 1 2
cols = 4×1
1 2 3 4
So, sds{i} is the same as [rows(i), cols(i)] but without repeats of the same value.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by