How do you find a specific row a certain value is at in a matrix?
10 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Dyuman Joshi
2023-8-25
编辑:Dyuman Joshi
2023-8-25
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))
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)
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)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!