Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Question regarding matrix index

5 ビュー (過去 30 日間)
jana
jana 2014 年 5 月 28 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hi,
I've a following matrix:
[1 2 3 4 5;
3 inf 5 7 inf;
5 6 7 inf inf;
2 3 4 9 1]
I wanted to find the minimum element from the last column with its row and column. So my answer to the above matrix should be 1 (as it is the minimum in column 5), row = 4, column = 5. I dont know how to code this. Please help!

回答 (3 件)

Sven
Sven 2014 年 5 月 28 日
編集済み: Sven 2014 年 5 月 28 日
Hi jana,
Try this:
X = [1 2 3 4 5;
3 inf 5 7 inf;
5 6 7 inf inf;
2 3 4 9 1]
[val, rowNum] = min(X,1) % Take the minimum in 1st dimension (columns)
val =
1 2 3 4 1
rowNum =
1 1 1 1 4
As you can see, the minimum value of each column is in val, and the row that it corresponds to is in the rowNum variable.
To get only the last (end) column you can use:
[val, rowNum] = min(X(:,end))
val =
1
rowNum =
4
  2 件のコメント
jana
jana 2014 年 5 月 28 日
but I want the index of the minimum value in the last column, not the entire matrix
Sven
Sven 2014 年 5 月 28 日
I updated the answer to show how to only consider the last column. Did that get the answer for you?

Image Analyst
Image Analyst 2014 年 5 月 28 日
編集済み: Image Analyst 2014 年 5 月 28 日
[minValueInLastCol, rowOfMin] = min(yourMatrix(:, end));
Or if you want the min in the last row (just for completeness in case you're interested):
[minValueInLastRow, colOfMin] = min(yourMatrix(end, :));

Geoff Hayes
Geoff Hayes 2014 年 5 月 28 日
If Z is your matrix from above, just do:
[minVal,minIdx] = min(Z(:,end));
This will return the minimum value in minVal and its index in that column in minIdx:
minVal =
1
minIdx =
4
You already know the column number (5, since last one) and the row number is simply minIdx.

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by