how can obtain min of matrix
2 次查看(过去 30 天)
显示 更早的评论
1 个评论
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
2015-1-26
[row,col]=find(matrix==min(nonzeros(matrix)));
2 个评论
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
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.
0 个评论
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));
0 个评论
sara
2015-1-26
编辑:sara
2015-1-26
2 个评论
Image Analyst
2015-1-26
I deleted my answer. Is there not any "Accept this answer" link for any of the others?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!