Effacer les filtres
Effacer les filtres

How do you find a specific row a certain value is at in a matrix?

4 vues (au cours des 30 derniers jours)
Milton
Milton le 25 Août 2023
Modifié(e) : Dyuman Joshi le 25 Août 2023
I have a matrix:Readings=cat(2,Time,Floors), which consists of
Time=[0700:100:1800]'
and
Floors=[5,13,10,14,42,49,46,67,64,32,12,4
8,17,28,39,66,71,74,154,126,57,20,7
2,8,15,24,14,51,48,68,145,55,11,2]';
I have obtained the maximum for each column through
Max_floor_1=max(Readings(:,2))
Max_floor_2=max(Readings(:,3))
Max_floor_3=max(Readings(:,4))
Now I want to see what position each maximum value have in each respective column and what time is at that specific poisition, in other words:where in column X is Max_floor_X and what value is has at that position, specifically what row. I think I can figure out how to extract the time when I know what row I am after.
Using [row,col]=find(Max_floor_1) returns
row =
1
col =
1
I am unsure on how to proceed after trying many different things.
Any tips in the right direction is appreciated!

Réponse acceptée

Dyuman Joshi
Dyuman Joshi le 25 Août 2023
Modifié(e) : Dyuman Joshi le 25 Août 2023
When you provide a numerical array to find, it gives the indices of all the nonzero values. And as Max_floor_2 is a non-zero scalar, it returns r=1, c=1.
Time=[0700:100:1800]';
Floors=[5,13,10,14,42,49,46,67,64,32,12,4
8,17,28,39,66,71,74,154,126,57,20,7
2,8,15,24,14,51,48,68,145,55,11,2]';
Readings=cat(2,Time,Floors);
If you want to obtain just the index of the 1st occurence of the maximum value (in case there are multiple maximum value present in the array), utilize the 2nd output of max
[Max_floor_1,idx1] = max(Readings(:,2))
Max_floor_1 = 67
idx1 = 8
In case, you want all the indices of occurence of maximum value, compare the maximum value to the array and then use find -
index = find(Readings(:,2)==Max_floor_1)
index = 8
Also, it's not a good practice to dynamically name variables, read - TUTORIAL: Why Variables Should Not Be Named Dynamically
Take advantage of the functionalities -
[Max_floor,index]=max(Readings(:,2:4),[],1)
Max_floor = 1×3
67 154 145
index = 1×3
8 8 9
  1 commentaire
Milton
Milton le 25 Août 2023
Thank you for a quick reply, I understand your process. Thank you for the article about naming variables, will adjust accordingly.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by