How to find the second closest value to a specific value from a given matrix
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Eranja Noopehewa
am 30 Okt. 2018
Bearbeitet: Stephen23
am 30 Okt. 2018
I have got a 18*12*6 matrix. From this matrix i want to find out the second closest value to 1.
0 Kommentare
Akzeptierte Antwort
Stephen23
am 30 Okt. 2018
Bearbeitet: Stephen23
am 30 Okt. 2018
Use sort instead of min, and pick as many as you want:
>> [v,x] = sort(abs(k(:)-1));
>> [p,n,m] = ind2sub(size(k),x(1:3)) % closest three
p =
6
3
6
n =
9
4
8
m =
2
2
4
>> k(p(1),n(1),m(1)) % (first) closest.
ans = 0.99869
>> k(p(2),n(2),m(2)) % second closest.
ans = 0.99852
>> k(p(3),n(3),m(3)) % third closest.
ans = 0.99852
Use linear indexing to easily access the values in k, here are the closest ten values:
>> k(x(1:10))
ans =
0.99869
0.99852
0.99852
0.99852
0.99834
0.99816
0.99781
1.01296
1.01311
1.01327
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Operators and Elementary Operations 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!