How to get the smallest difference(Array)

>>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 件のコメント

KSSV
KSSV 2017 年 8 月 31 日
Please not that....._min_ gives you only minimum value....so output is only one value. What you are expecting?
KL
KL 2017 年 8 月 31 日
What's the point of uploading that picture? You expect us to read it this way?
José-Luis
José-Luis 2017 年 8 月 31 日
Where on god's green earth is abs(10-8) equal to abs(10-4)?
José-Luis
José-Luis 2017 年 8 月 31 日
Let me rephrase. Unless something very basic has changed, there is no way that
2 == 4
You probably meant E(6) (just dawned on me, I need coffee). If that's the case, my answer should solve your problem.
José-Luis
José-Luis 2017 年 8 月 31 日
Well then I don't understand what you are trying to achieve.
Cedric
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:
Jan
Jan 2017 年 9 月 17 日
編集済み: Jan 2017 年 9 月 17 日
I have reverted the question. Please, Shivam Prasad, do not remove the question after somebody has spent the time to provide an answer.

サインインしてコメントする。

 採用された回答

José-Luis
José-Luis 2017 年 8 月 31 日

0 投票

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)

その他の回答 (1 件)

Jan
Jan 2017 年 9 月 17 日
編集済み: Jan 2017 年 9 月 17 日

1 投票

The question does not contain the detail, that the input is a matrix, but a scalar and a vector only. Then:
[c, index] = min(abs(Vec - E));
is almost a solution. Do it in two steps:
Dist = abs(Vec - E);
and then use this to determine the indices:
Dist == min(Dist)

カテゴリ

ヘルプ センター および File ExchangeImage Processing Toolbox についてさらに検索

製品

質問済み:

2017 年 8 月 31 日

編集済み:

2017 年 9 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by