Finding the Index number that corresponds to a specific value

Can any one help me how can i determine the index number that coresponds to a value in a matrix? for example, I've this : >> [val2 index2] = max(max(C_IA))
val2(:,:,1) =
31.3781 + 0.0000i
val2(:,:,2) =
30.0139 - 0.0000i
val2(:,:,3) =
27.7915 + 0.0000i
index2(:,:,1) =
1
index2(:,:,2) =
1
index2(:,:,3) =
3
how can i find index2 value which corresponds to the maximum of val2?
in our case here, the solution is to get index2 = 1 as it corresponds to maximum of val2 = 31.3781 but how can i do it in matlab?
I was trying to write something like find(index2 == max(val2)) but it doesn't work.

2 commentaires

Dear all, thanks alot for your responses however this is not what I want. May be I was not so clear in my question so let me explain it agian. What I want is not to find the index of max(C_IA), I will paste below my Input and then state what I want to get: I've originally the following matrix: >> C_IA
C_IA(:,:,1) =
31.3781 + 0.0000i 26.0762 - 0.0000i 29.2083 - 0.0000i
30.1921 - 0.0000i 27.8010 + 0.0000i 26.0254 - 0.0000i
27.9994 - 0.0000i 26.7921 - 0.0000i 27.7583 + 0.0000i
C_IA(:,:,2) =
29.1817 + 0.0000i 28.4916 + 0.0000i 28.6310 + 0.0000i
30.0139 - 0.0000i 29.9155 + 0.0000i 27.0452 - 0.0000i
28.4040 + 0.0000i 27.7000 + 0.0000i 28.3678 + 0.0000i
C_IA(:,:,3) =
26.7694 - 0.0000i 24.7692 - 0.0000i 27.7915 + 0.0000i
26.8899 - 0.0000i 26.0439 - 0.0000i 24.7235 - 0.0000i
26.9859 + 0.0000i 24.8153 + 0.0000i 26.2597 - 0.0000i
Please note that the above output was my result I wanted of a three nested for loops (for K1=1:3 then for K2=1:3 then for K3=1:3)
then I calculated the index of the max of C_IA on three steps in order to know which K1,K2 and K3 values has achived the max(C_IA(:))
[val1 index1] = max(C_IA) [val2 index2] = max(max(C_IA)) [val3 index3] = max(max(max(C_IA)))
when I run the above, I get the following for the first line: >> [val1 index1] = max(C_IA)
val1(:,:,1) =
31.3781 + 0.0000i 27.8010 + 0.0000i 29.2083 - 0.0000i
val1(:,:,2) =
30.0139 - 0.0000i 29.9155 + 0.0000i 28.6310 + 0.0000i
val1(:,:,3) =
26.9859 + 0.0000i 26.0439 - 0.0000i 27.7915 + 0.0000i
index1(:,:,1) =
1 2 1
index1(:,:,2) =
2 2 1
index1(:,:,3) =
3 2 1
and as we have max(C_IA(:)) = 31.3781 then I want to get the first element value of index1 that corresponds to 31.3781 (in this case index1=1
Same for the 2nd step: >> [val2 index2] = max(max(C_IA))
val2(:,:,1) =
31.3781 + 0.0000i
val2(:,:,2) =
30.0139 - 0.0000i
val2(:,:,3) =
27.7915 + 0.0000i
index2(:,:,1) =
1
index2(:,:,2) =
1
index2(:,:,3) =
3
I wanted to get index2 value that corresponds to 31.3781 value (in this case index2=1)
for the last step [val3 index3] = max(max(max(C_IA))) it's clear since what i want is exactly index3 output.
My question is: how can i get index2 value that corresponds to 31.3781 in this case?

Connectez-vous pour commenter.

 Réponse acceptée

Sean de Wolski
Sean de Wolski le 21 Nov 2013
Modifié(e) : Sean de Wolski le 22 Nov 2013
EDIT
If you want the dimensional indices of the maximum value in the matrix, use max and ind2sub
x = rand(3,3,3);
[mxv,idx] = max(x(:));
[row,col,pag] = ind2sub(size(x),idx)
And for more info, as Bjorn has pointed out:
doc ind2sub

2 commentaires

thanks alot Sean de Wolski , your simple two lines has solved my problem .. amazing, thanks sooooooooo much :) :)

Connectez-vous pour commenter.

Plus de réponses (1)

Bruno Pop-Stefanov
Bruno Pop-Stefanov le 21 Nov 2013
Modifié(e) : Brewster le 21 Nov 2013
You can use the find function on your input matrix directly. If I am correct, C_IA should be 3-dimensional. In your case, you would write:
maxval = max(max(max(C_IA)));
index = find(C_IA == maxval);
'index' is the linear index of the max of C_IA. You can then access the element by writing
C_IA(index)
or you can transform this linear index into subscripts using ind2sub.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by