picking maximum difference across entries of row vectors, Part 3

조회 수: 7 (최근 30일)
The question is
for each row, take a difference between every possible pair of two elements in the row and returns the maximum difference
Now A can have missing values. For example, A =[ NaN NaN 3 9 4]. In this case the previous solution
vec = sort(A);
B = vec(end)-vec(1);
does not return a correct answer 6. Please advise.
  댓글 수: 2
alpedhuez
alpedhuez 2018년 6월 6일
Suppose I replace NaN by -infty. Then the problem would be about taking the difference between max and min among nonzero elements for each row.
alpedhuez
alpedhuez 2018년 6월 7일
편집: alpedhuez 2018년 6월 7일
I can run a loop for each row calculates
max(D(i,:),[],'omitnan')-min(D(i,:),[],'omitnan');
but would hope simpler code.

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

채택된 답변

Kaushik Lakshminarasimhan
Kaushik Lakshminarasimhan 2018년 6월 7일
A = [NaN NaN 3 9 4 ; 1 100 NaN 34 12];
maxdiffs = max(sort(A,2),[],2) - min(sort(A,2),[],2);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by