# How do I find the biggest difference in an array?

104 views (last 30 days)
AelinAG on 7 Sep 2018
Answered: Image Analyst on 8 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?

madhan ravi on 7 Sep 2018
AN EXAMPLE?
AelinAG on 7 Sep 2018
[62 1 18] The biggest difference is here 61, but since the smallest element goes first the solution would be 17
madhan ravi on 7 Sep 2018
You mean the difference between the two elements 1 and 18 because we just ignore the max value ?

Matt J on 7 Sep 2018
Edited: 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) )
Matt J on 8 Sep 2018
90 is correct. Not 400. Remember, allowable differences A-B are those in which B precedes A in the list.
Paolo on 8 Sep 2018
Ah yes that is what OP is asking for, read Image Analyst's comment and got confused

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).

Paolo on 7 Sep 2018
Edited: Paolo on 7 Sep 2018
>> A=[62 1 0];
>> max(reshape((diff(perms(A))),1,[]))
62
>> A = [62, 10, 18, 100, 4, -300];
>> max(reshape((diff(perms(A))),1,[]))
400

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.