how can obtain min of matrix

조회 수: 2 (최근 30일)
sara
sara 2015년 1월 25일
편집: David Young 2015년 1월 26일
  댓글 수: 1
Matz Johansson Bergström
Matz Johansson Bergström 2015년 1월 25일
편집: Matz Johansson Bergström 2015년 1월 26일
Please write the question in text and not as a attached image. The description of your question will not be indexed by the Mathworks search engine if you provide an image.

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

답변 (5개)

Matt J
Matt J 2015년 1월 26일
[row,col]=find(matrix==min(nonzeros(matrix)));
  댓글 수: 2
sara
sara 2015년 1월 26일
thanks Matt
Matz Johansson Bergström
Matz Johansson Bergström 2015년 1월 26일
Ah nonzeros , didn't even know it existed. This is the shortest and best solution and should be accepted as the answer. Good job Matt.

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


Matz Johansson Bergström
Matz Johansson Bergström 2015년 1월 25일
편집: Matz Johansson Bergström 2015년 1월 25일
This solution is maybe a little ugly, but it works Say that A is the matrix.
tmp = A; %we will destroy elements, so we store A in tmp
tmp(tmp==0) = []; %get rid of 0-elements
val = min(min(tmp)); %find value of the smallest element
[u,v] = find(A==val, 1); %find the position
u and v is the (first) row and column of the index of the smallest element in A. The smallest element could occur several times in the matrix.

David Young
David Young 2015년 1월 25일
편집: David Young 2015년 1월 26일
Another approach:
tmp = A; % avoid destroying A
tmp(tmp == 0) = Inf; % make zero elements bigger than non-zeros
[minVal, minIndex] = min(tmp(:)); % find min value, linear index
[minRow, minCol] = ind2sub(size(A), minIndex); % convert to subscripts
or if you prefer
tmp = A;
tmp(tmp == 0) = Inf; % as above
[colminvals, colminrows] = min(tmp); % find min in each column
[minVal, minCol] = min(colminvals); % find overall min and its column
minRow = colminrows(minCol); % select row of overall min
or my personal preferred method, avoiding copying the matrix and also avoiding a repeat scan with the find operation:
nzpos = A ~= 0;
indexes = 1:numel(A);
indnz = indexes(nzpos);
[minVal, minIndnz] = min(A(nzpos));
[minRow, minCol] = ind2sub(size(A), indnz(minIndnz));

sara
sara 2015년 1월 26일
thanks
  댓글 수: 6
Matz Johansson Bergström
Matz Johansson Bergström 2015년 1월 26일
Very good.
sara
sara 2015년 1월 26일
편집: sara 2015년 1월 26일
ohhh yes I just tried it for my example. and I was in a hurry and ...thanks dear David and Matz...

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


sara
sara 2015년 1월 26일
편집: sara 2015년 1월 26일
Dear ImageAnalyst
I accepted your answer becuase I was in hurry and I check it just for one example...I can not do any changes if you can please edit this...
thanks
  댓글 수: 2
Image Analyst
Image Analyst 2015년 1월 26일
I deleted my answer. Is there not any "Accept this answer" link for any of the others?
sara
sara 2015년 1월 26일
thanks

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by