how can obtain min of matrix

1 Kommentar

Matz Johansson Bergström
Matz Johansson Bergström am 25 Jan. 2015
Bearbeitet: Matz Johansson Bergström am 26 Jan. 2015
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.

Melden Sie sich an, um zu kommentieren.

Antworten (5)

Matt J
Matt J am 26 Jan. 2015

3 Stimmen

[row,col]=find(matrix==min(nonzeros(matrix)));

2 Kommentare

sara
sara am 26 Jan. 2015
thanks Matt
Matz Johansson Bergström
Matz Johansson Bergström am 26 Jan. 2015
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.

Melden Sie sich an, um zu kommentieren.

Matz Johansson Bergström
Matz Johansson Bergström am 25 Jan. 2015
Bearbeitet: Matz Johansson Bergström am 25 Jan. 2015

0 Stimmen

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 am 25 Jan. 2015
Bearbeitet: David Young am 26 Jan. 2015

0 Stimmen

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 am 26 Jan. 2015

0 Stimmen

thanks

6 Kommentare

David Young
David Young am 26 Jan. 2015
You're welcome, Sara - but it's not helpful to other users to accept an incorrect answer!
sara
sara am 26 Jan. 2015
but I use the code that image analyst give me and it works ???
I comfused can you explane this for me please ?? I want to use a code like your code but I saw that image Analyst code is shorter and worked well
thanks David
Matz Johansson Bergström
Matz Johansson Bergström am 26 Jan. 2015
Please check John D'Erricos comment to Image Analysts solution above. He even gives the counter-example to why it doesn't work.
I deleted my incorrect answer so she can accept someone else's answer (as there is no current way to "Unaccept" an answer other than by deleting it). By the way, in case anyone was wondering it was:
[minValue, index] = min(yourMatrix(yourMatrix ~= 0));
[row, column] = ind2sub(size(yourMatrix), index);
but John D'Errico pointed out that it's wrong because the index you get when you extract the non-zero elements is not the same as the index it would be in the original full array.
Matz Johansson Bergström
Matz Johansson Bergström am 26 Jan. 2015
Very good.
sara
sara am 26 Jan. 2015
Bearbeitet: sara am 26 Jan. 2015
ohhh yes I just tried it for my example. and I was in a hurry and ...thanks dear David and Matz...

Melden Sie sich an, um zu kommentieren.

sara
sara am 26 Jan. 2015
Bearbeitet: sara am 26 Jan. 2015

0 Stimmen

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 Kommentare

Image Analyst
Image Analyst am 26 Jan. 2015
I deleted my answer. Is there not any "Accept this answer" link for any of the others?
sara
sara am 26 Jan. 2015
thanks

Melden Sie sich an, um zu kommentieren.

Kategorien

Tags

Gefragt:

am 25 Jan. 2015

Bearbeitet:

am 26 Jan. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by