MATLAB Answers

0

picking maximum difference across entries of row vectors

Asked by alpedhuez on 6 Jun 2018
Latest activity Edited by Stephen Cobeldick on 6 Jun 2018
I have a matrix A. I would like to create a column vector B that is
for each row, take a difference between every possible pair of two elements in the row and returns the maximum difference
For example, if A= [1 3 6 7 10], then B=[9]. Please advise a compact way to drive B.

  0 Comments

Sign in to comment.

Products

1 Answer

Answer by Paolo
on 6 Jun 2018
Edited by Paolo
on 6 Jun 2018
 Accepted Answer

Perhaps a simpler solution which only requires a sort:
A = [1 3 6 7 10];
A = sort(A);
B = A(end) - A(1);
B =
9
Example 1:
A= [6 3 324 2 123];
A = sort(A);
B = A(end) - A(1);
B =
322
Example 2:
A = [111 34 88 11 12];
A = sort(A);
B = A(end) - A(1);
B =
100

  3 Comments

Here A is a matrix with multiple rows not just a row vector.
@alpedhuez: sort the rows, and then subtract the first column from the last column.

Sign in to comment.