how to find index of matrix if value is known?

2 ビュー (過去 30 日間)
Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2018 年 10 月 5 日
コメント済み: Image Analyst 2021 年 4 月 11 日
Kv= 212.640075234138 212.941159818301 213.241495908758 213.541083505509
211.539179873813 211.843738655644 212.147548943768 212.450610738187
210.439033007195 210.747065986692 211.054350472484 211.360886464570
209.339634634282 209.651141811447 209.961900494907 210.271910684660
208.240984755075 208.555966129908 208.870199011035 209.183683398456
207.143083369574 207.461538942074 207.779246020869 208.096204605957
206.045930477779 206.367860247947 206.689041524409 207.009474307164
NaN NaN NaN NaN
NaN NaN NaN NaN
If i know thw column is 1 and min value is 206 how would i find row index?
  3 件のコメント
jonas
jonas 2018 年 10 月 5 日
the second output of the min() function is the index
Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2018 年 10 月 5 日
HELLO RIK yes i want to know the min value of a specific column
as the input would be column
and as a output
the minimum value and row is expected

サインインしてコメントする。

採用された回答

KSSV
KSSV 2018 年 10 月 5 日
編集済み: KSSV 2018 年 10 月 5 日
[val,idx] = min(kv(:,1))
Or if you want (row,col) of the minimum element in a matrix use:
A = rand(3) ;
[val,idx] = min(A(:)) ;
[i,j] = ind2sub(size(A),idx) ; % row, column position of min
[A(i,j) val]
  6 件のコメント
Image Analyst
Image Analyst 2018 年 10 月 5 日
You accepted this, so does it work or not?
I would not use min() or ind2sub() to find the row. I'd use find() like in my solution. Take a look at it.
Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2018 年 10 月 5 日
yes it worked for my case

サインインしてコメントする。

その他の回答 (3 件)

Luna
Luna 2018 年 10 月 5 日
編集済み: Luna 2018 年 10 月 5 日
[minValue, indexofMin] = min(Kv(:,1)) % for the first column
[minValue, indexofMin] = min(Kv) % for all columns

Image Analyst
Image Analyst 2018 年 10 月 5 日
I would not use min() to get the index because it finds ONLY the first occurrence of the min and if the min occurs more than once, it will miss it. I'd use find, like in this demo:
Kv = [212.640075234138 212.941159818301 213.241495908758 213.541083505509
211.539179873813 211.843738655644 212.147548943768 212.450610738187
210.439033007195 210.747065986692 211.054350472484 211.360886464570
209.339634634282 209.651141811447 209.961900494907 210.271910684660
208.240984755075 208.555966129908 208.870199011035 209.183683398456
207.143083369574 207.461538942074 207.779246020869 208.096204605957
206.045930477779 206.367860247947 206.689041524409 207.009474307164
NaN NaN NaN NaN
NaN NaN NaN NaN
212.640075234138 212.941159818301 213.241495908758 213.541083505509
211.539179873813 211.843738655644 212.147548943768 212.450610738187
210.439033007195 210.747065986692 211.054350472484 211.360886464570
209.339634634282 209.651141811447 209.961900494907 210.271910684660
208.240984755075 208.555966129908 208.870199011035 209.183683398456
207.143083369574 207.461538942074 207.779246020869 208.096204605957
206.045930477779 206.367860247947 206.689041524409 207.009474307164
NaN NaN NaN NaN
NaN NaN NaN NaN]
% Find min value in column 1
minValue = min(Kv(:, 1))
% Use find() instead of min() to get the row
% because the min may occur more than once.
rows = find(Kv(:, 1) == minValue)
You'll see
minValue =
206.045930477779
rows =
7
16
find() finds both occurrences of the min in column 1, not just the first one like min() would.
  1 件のコメント
Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2018 年 10 月 5 日
thats nice.. thank you for the info.

サインインしてコメントする。


Vikas Singh
Vikas Singh 2021 年 4 月 11 日
how i can find the index of matrix if i know the value of matrix ??
  5 件のコメント
Vikas Singh
Vikas Singh 2021 年 4 月 11 日
Thank you it works for me
Image Analyst
Image Analyst 2021 年 4 月 11 日
@Rik, I don't think it's subject to rounding errors if the value is actually in the matrix and not one you arrived at some other way. For example
value = max(m(:)) % Gauranteed to be in m and exactly match.
[rows, columns] = find(m == value)
Now it should always find all occurrences as far as I know. Of course if the max was 1.00000000001 and you put m == 1, then of course the value 1 is not in there exactly and rows and columns will be empty. But @Vikas Singh said that he does "know the value of matrix" exactly so it should work if the value actually came from the matrix.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeArray and Matrix Mathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by