picking maximum difference across entries of row vectors

조회 수: 2 (최근 30일)
alpedhuez
alpedhuez 2018년 6월 6일
편집: Stephen23 2018년 6월 6일
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.

채택된 답변

Paolo
Paolo 2018년 6월 6일
편집: Paolo 2018년 6월 6일
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
alpedhuez
alpedhuez 2018년 6월 6일
Here A is a matrix with multiple rows not just a row vector.
Stephen23
Stephen23 2018년 6월 6일
편집: Stephen23 2018년 6월 6일
@alpedhuez: sort the rows, and then subtract the first column from the last column.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by