how to find index of matrix if value is known?
2 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
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
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.
その他の回答 (3 件)
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.
Vikas Singh
2021 年 4 月 11 日
how i can find the index of matrix if i know the value of matrix ??
5 件のコメント
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 Exchange で Array and Matrix Mathematics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!