How to get the smallest difference(Array)
43 次查看(过去 30 天)
显示 更早的评论
>>The issue i'm having is that if there are two values of equal difference only the closest one is displayed. Can someone please correct my coding.
function[smallestPosition] = FindSmallestElevationChange(i,B)
E = [ 3 6 5 4 7 8 3 ; 5 7 6 2 3 1 7 ; 6 8 5 8 5 4 8 ; 7 4 9 7 6 5 3 ];
*%B = 3 adjacent values e.g.. if I chose E(10), B = [ 4 2 8 ]
%the smallest difference should be 4 and 8 which is (1 and 3) respectively
%since there is a difference of 2 for both of them.*
E(i);
[c index] = min(abs(B-E(i)));
the output given is only 1(which is 4)
[REVERTED, was edited away by the OP, Jan]
7 个评论
Cedric
2017-9-17
编辑:Cedric
2017-9-17
Shivam Prasad, the least you can do if you don't need help anymore, is to accept the answer of whoever was nice enough to give you freely part of his/her time.
I accepted the answer from José-Luis (before Jan restored the original question and answered).
The question was originally titled "How to get the smallest difference(Array)" and it had the following picture attached:
采纳的回答
José-Luis
2017-8-31
bla = randi(50,10);
val = 25;
result = abs(bla-val);
result = result == min(result(:));
[xx,yy] = ndgrid(1:size(bla,1),1:size(bla,2));
idx_x = xx(result)
idx_y = yy(result)
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!