Asked by AelinAG
on 7 Sep 2018

If I have an array, and I have to find the biggest difference, but the smallest element HAS to be before the biggest element, what do I do?

Answer by Matt J
on 7 Sep 2018

Edited by Matt J
on 8 Sep 2018

If only consecutive differences are to be considered,

A=[62 1 18];

diffs= diff(A);

result=max( diffs(diffs>=0) )

EDIT :

If they can be non-consecutive,

diffs=triu( A(:).'- A(:) );

result=max( diffs(diffs>=0) )

Paolo
on 7 Sep 2018

A = [62, 10, 18, 100, 4, -300];

diffs=triu( A(:).'- A(:) );

result=max( diffs(diffs>=0) )

returns 90 instead of 400

Matt J
on 8 Sep 2018

Paolo
on 8 Sep 2018

Ah yes that is what OP is asking for, read Image Analyst's comment and got confused

Sign in to comment.

Answer by Image Analyst
on 7 Sep 2018

The code below will work even if the biggest difference is not in adjacent elements, and if the values are negative.

A = [62, 10, 18, 100, 4, -300];

% The biggest difference is between element 4 and 6 and has a distance of 400.

distances = pdist2(A', A')

maxDistance = max(distances(:))

[rows, columns] = find(distances == maxDistance)

index1 = rows(1);

index2 = columns(1);

% Swap if necessary

if index1 > index2

[index1, index2] = deal(index2, index1)

end

message = sprintf('The biggest difference is between\nelement #%d (%f) and\nelement #%d (%f)\nwith a difference of %f',...

index1, A(index1), index2,A(index2), A(index1)-A(index2));

fprintf('%s\n', message);

uiwait(helpdlg(message));

You'll see

distances =

0 52 44 38 58 362

52 0 8 90 6 310

44 8 0 82 14 318

38 90 82 0 96 400

58 6 14 96 0 304

362 310 318 400 304 0

maxDistance =

400

rows =

6

4

columns =

4

6

index1 =

4

index2 =

6

The biggest difference is between

element #4 (100.000000) and

element #6 (-300.000000)

with a difference of 400.000000

It uses pdist2() in the Statistics and Machine Learning Toolbox to compute the difference of every element from every element (including itself).

Sign in to comment.

Answer by Image Analyst
on 8 Sep 2018

If you want to find the biggest difference in the upwards direction only, ignoring any differences in the downward direction (where the index of the bigger value is less than the index of the smaller value), and want to allow elements that don't need to be adjacent to each other, this code will work:

% Make vector where biggest difference in the upwards direction is between index 2 and 6.

% Note there is a bigger difference in the downward direction

% between elements 5 and 6, but we don't care about that.

A = [130, 85, 110, 100, 115, 80, 105]

plot(A, 'b*-', 'LineWidth', 2, 'MarkerSize', 14);

grid on;

xlabel('Index', 'FontSize', 20);

ylabel('Value', 'FontSize', 20);

[rows, columns] = size(A)

for row = 1 : length(A)

for col = 1 : length(A)

distances(row, col) = A(row) - A(col);

end

end

distances % Print to command window.

% Consider only positive differences

distances = tril(distances)

maxDistance = max(distances(:))

[rows, columns] = find(distances == maxDistance)

index1 = rows(1);

index2 = columns(1);

% Swap if necessary

if index1 > index2

[index1, index2] = deal(index2, index1)

end

% Put a circle around the pair we found on the plot.

hold on;

plot([index1, index2], [A(index1), A(index2)], 'ro', 'MarkerSize', 20, 'LineWidth', 2);

message = sprintf('The biggest difference is between\nelement #%d (%.1f) and\nelement #%d (%.1f)\nwith a difference of %.1f',...

index1, A(index1), index2,A(index2), A(index2)-A(index1));

fprintf('%s\n', message);

uiwait(helpdlg(message));

Note how it finds elements 2 and 5 with a difference of 30 even though elements 5 and 6 have a difference of 35, but the bigger value occurs at index 5, which comes before the index of the smaller value at index 6, so I ignore that pair.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## madhan ravi (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/418075-how-do-i-find-the-biggest-difference-in-an-array#comment_607819

## AelinAG (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/418075-how-do-i-find-the-biggest-difference-in-an-array#comment_607821

## madhan ravi (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/418075-how-do-i-find-the-biggest-difference-in-an-array#comment_607826

Sign in to comment.