Azzera filtri
Azzera filtri

how to find index of matrix if value is known?

1 visualizzazione (ultimi 30 giorni)
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 Commenti
jonas
jonas il 5 Ott 2018
the second output of the min() function is the index
Shubham Mohan Tatpalliwar
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

Accedi per commentare.

Risposta accettata

KSSV
KSSV il 5 Ott 2018
Modificato: KSSV il 5 Ott 2018
[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 Commenti
Image Analyst
Image Analyst il 5 Ott 2018
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.

Accedi per commentare.

Più risposte (3)

Luna
Luna il 5 Ott 2018
Modificato: Luna il 5 Ott 2018
[minValue, indexofMin] = min(Kv(:,1)) % for the first column
[minValue, indexofMin] = min(Kv) % for all columns

Image Analyst
Image Analyst il 5 Ott 2018
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
Vikas Singh il 11 Apr 2021
how i can find the index of matrix if i know the value of matrix ??
  5 Commenti
Image Analyst
Image Analyst il 11 Apr 2021
@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.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by