Asked by Shivam Prasad
on 31 Aug 2017

>>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]

Answer by Jan
on 17 Sep 2017

Edited by Jan
on 17 Sep 2017

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)

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 7 Comments

## KSSV (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/354671-how-to-get-the-smallest-difference-array#comment_480829

## KL (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/354671-how-to-get-the-smallest-difference-array#comment_480871

## José-Luis (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/354671-how-to-get-the-smallest-difference-array#comment_480878

## José-Luis (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/354671-how-to-get-the-smallest-difference-array#comment_480886

## José-Luis (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/354671-how-to-get-the-smallest-difference-array#comment_480891

## Cedric Wannaz (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/354671-how-to-get-the-smallest-difference-array#comment_484981

## Jan (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/354671-how-to-get-the-smallest-difference-array#comment_485022

Sign in to comment.