finding distance of distinct value
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi, suppose I have a matrix as follows:
I =
1 1 5 1 1 1 8
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 2 1 1 1
1 1 1 1 1 6 1
1 1 3 1 1 1 1
I(3,4) is 1 . I need to know the distance of this one from the distinct value of this matrix.i.e distances of 2,3,5,6,8. How to do this easily? and also need the matrix position of that value.Thanks in advance.
0 Kommentare
Akzeptierte Antwort
Andrei Bobrov
am 18 Mai 2011
variant
o34 = zeros(size(I));
o34(3,4)=1;
o34d = bwdist(o34);
distato34 = [I(I~=1) o34d(I~=1)]
more variant
o = [3 4];
dC = arrayfun(@(x)(cumsum(ones(size(I)),x)-o(x)).^2,1:2,'un',0);
d = sqrt(dC{1} + dC{2});
l = I~=1;
out = [I(l) d(l)]
more more variant
[Hi Hj] = find(I~=1);
out = [I(I~=1) sqrt(sum(bsxfun(@minus,[Hi Hj],[3 4]).^2,2))]
0 Kommentare
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!