필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
Milton
Milton 2023년 8월 25일
편집: Dyuman Joshi 2023년 8월 25일
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!

채택된 답변

Dyuman Joshi
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))
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
Milton
Milton 2023년 8월 25일
Thank you for a quick reply, I understand your process. Thank you for the article about naming variables, will adjust accordingly.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by