필터 지우기
필터 지우기

Max/Min of nonzero rows/cols

조회 수: 11 (최근 30일)
Edward Umpfenbach
Edward Umpfenbach 2012년 4월 12일
댓글: Dan 2015년 12월 28일
I have a Matrix, A.
I want to find the max and min for every row and column in A, excluding the zero entries.
I have coded it using For loops, but it is too slow. I need it to be all vectorized. Any help is greatly appreciated.
  댓글 수: 1
Dan
Dan 2015년 12월 28일
A = matrix with some zero entries . Indzero = find( A== 0); B=A; B(Indzero) = max(max(A))+1; Min(B,[],1) B(Indzero) = min(min(A))-1; Max(B,[],1)
U can use this sort of logic maybe . u can also go at it by gpu processing or mexing for faster results ( this is exactly the type of probelms where u get the best rests with gpus Nd mexing )

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

채택된 답변

Richard Brown
Richard Brown 2012년 4월 12일
B = A;
B(B == 0) = NaN;
colMax = max(B);
colMin = min(B);
rowMax = max(B, [], 2);
rowMin = min(B, [], 2);
edit: missed the bit about the zero entries
  댓글 수: 1
Edward Umpfenbach
Edward Umpfenbach 2012년 4월 12일
Thank you. I didn't think to change the zeros to NaN so that they didn't mess up the min operation.

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

추가 답변 (1개)

Geoff
Geoff 2012년 4월 12일
When you say 'for every row and column', do you mean a single max and min for the entire matrix? This would do it:
nzmin = min(A(A ~= 0));
nzmax = max(A(A ~= 0));
[edit]
Note that if you have float data, you might want to consider values 'near' zero:
ztol = eps(max(A(:)));
nz = A < -ztol | A > ztol; % select non-zeros
nzmin = min(A(nz));
nzmax = max(A(nz));
It's refreshing to use a proliferation of 'nz', since it's my nationality =)
  댓글 수: 3
Geoff
Geoff 2012년 4월 12일
My heart's in Dunedin. But I moved up to Auckland to get away from Animation Research Ltd ;-) You're a Canterbury lad, right?
Richard Brown
Richard Brown 2012년 4월 12일
I am indeed, wobbles and all :)

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by