필터 지우기
필터 지우기

max value in each row with its index

조회 수: 17 (최근 30일)
kurdistan mohsin
kurdistan mohsin 2022년 6월 7일
편집: kurdistan mohsin 2022년 6월 8일
hi , i have the below matrix , and i want to find the maximun value with its location(index)at each row w, so after finding the maximum value of each row then i will find maximum of maximums of these values and save it in another matirx with the same location(index) and all other values are zeros
inputmatrix= 8.0000 0 7.3398 0 8.0000
1.6635 0.7103 3.2000 3.2000 3.2000
3.2953 3.2000 3.2000 3.2000 3.2000
16.0000 16.0000 16.0000 10.8963 7.6226
8.2107 8.0000 0 5.3333 5.3333
0 3.2000 1.1829 3.2000 3.2000
8.0000 4.9461 0 14.2047 8.0000
1.4528 16.0000 16.0000 16.0000 3.3661
8.0000 8.0000 4.0000 4.0000 4.0000
0 2.5510 3.0116 2.6667 2.6667

답변 (2개)

Davide Masiello
Davide Masiello 2022년 6월 7일
편집: Davide Masiello 2022년 6월 7일
inputmatrix= [8.0000 0 7.3398 0 8.0000;...
1.6635 0.7103 3.2000 3.2000 3.2000;...
3.2953 3.2000 3.2000 3.2000 3.2000;...
16.0000 16.0000 16.0000 10.8963 7.6226;...
8.2107 8.0000 0 5.3333 5.3333;...
0 3.2000 1.1829 3.2000 3.2000;...
8.0000 4.9461 0 14.2047 8.0000;...
1.4528 16.0000 16.0000 16.0000 3.3661;...
8.0000 8.0000 4.0000 4.0000 4.0000;...
0 2.5510 3.0116 2.6667 2.6667];
To find the maximum of each row and their indexes
[row_max,col_idx] = max(inputmatrix,[],2)
row_max = 10×1
8.0000 3.2000 3.2953 16.0000 8.2107 3.2000 14.2047 16.0000 8.0000 3.0116
col_idx = 10×1
1 3 1 1 1 2 4 2 1 3
I am not sure what you wanna do in the next step though.
  댓글 수: 4
kurdistan mohsin
kurdistan mohsin 2022년 6월 7일
thank you! , how can i index each value with its row and coumn index ?
Jan
Jan 2022년 6월 7일
index = sub2ind(size(inputmatrix), col_idx.', 1:5);
inputmatrix(index) % same values as row_max

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


Steven Lord
Steven Lord 2022년 6월 7일
Do you actually need or want the locations of the maximum element in each row or is that just a temporary step towards your ultimate goal of identifying the largest value in the whole matrix? If the latter, consider an alternate approach. Let's take some sample data.
A = randi([-10 10], 5)
A = 5×5
-4 -6 10 -5 -2 -10 -2 6 1 0 -3 -7 3 1 9 -5 -7 4 -9 -1 -5 -4 -6 7 -2
Determine the maximum value in the array (stored as value) and its linear index (stored as location.) Using the 'linear' option requires release R2019a or later.
[value, location] = max(A, [], 'all', 'linear')
value = 10
location = 11
Create an all zero array the same size as A.
B = zeros(size(A));
Set the element in B corresponding to the location of the maximum value in A to that maximum value.
B(location) = value
B = 5×5
0 0 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  댓글 수: 1
kurdistan mohsin
kurdistan mohsin 2022년 6월 8일
편집: kurdistan mohsin 2022년 6월 8일
this is a great solution , but what if the location was indicated as it's row and cloumn indexing ,like in your example i want to give me the location as ; [1,3] , is it possible?

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by