How to find the distance between two non-zero (the element is a one) elements in a matrix
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a matrix with entries as "1" at random places and i need to find (a) find its non-zero neighbours (b) find the euclidean distance between its non-zero neighbours and the pixel itself
0 Kommentare
Akzeptierte Antwort
Andrei Bobrov
am 17 Feb. 2012
use function pdist from Statistics Toolbox
a = rand(10)<.07
[i1 j1] = find(a);
out = tril(ones(numel(i1)),-1)
out(out~=0) = hypot(pdist(i1),pdist(j1))
OR without pdist
[i1 j1] = find(a);
idx = [i1 j1];
ij = arrayfun(@(ii)tril(bsxfun(@minus,idx(:,ii),idx(:,ii).'),-1),1:2,'un',0);
out = hypot(ij{:})
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Statistics and Machine Learning Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!