could anyone help me how to select four maximum values of each row in a matrix size(10,8)
7 views (last 30 days)
Show older comments
jaah navi
on 29 May 2019
Commented: Walter Roberson
on 29 May 2019
I am having a matrix of size(10,8) (% 10 rows and 8 columns)
I want to select 4 maximum values from each row by having the result of matrix size(4,8)(%4 rows and 8 columns)
Could anyone please help me on this.
1 Comment
Accepted Answer
KALYAN ACHARJYA
on 29 May 2019
Edited: KALYAN ACHARJYA
on 29 May 2019
You can do it in multiple ways, let’s consider, the mat1 is matrix having size 10x8
sort_mat=sort(mat1);
result=sort_mat(7:end,:);
Another way as per @madhan ravi comment
sort_mat=sort(mat1,'descend');
result=sort_mat(1:4,:)
See example
>> mat1=rand(10,8)
mat1 =
0.2599 0.5499 0.1839 0.3692 0.5752 0.6477 0.7802 0.8176
0.8001 0.1450 0.2400 0.1112 0.0598 0.4509 0.0811 0.7948
0.4314 0.8530 0.4173 0.7803 0.2348 0.5470 0.9294 0.6443
0.9106 0.6221 0.0497 0.3897 0.3532 0.2963 0.7757 0.3786
0.1818 0.3510 0.9027 0.2417 0.8212 0.7447 0.4868 0.8116
0.2638 0.5132 0.9448 0.4039 0.0154 0.1890 0.4359 0.5328
0.1455 0.4018 0.4909 0.0965 0.0430 0.6868 0.4468 0.3507
0.1361 0.0760 0.4893 0.1320 0.1690 0.1835 0.3063 0.9390
0.8693 0.2399 0.3377 0.9421 0.6491 0.3685 0.5085 0.8759
0.5797 0.1233 0.9001 0.9561 0.7317 0.6256 0.5108 0.5502
>> sort_mat=sort(mat1)
sort_mat =
0.1361 0.0760 0.0497 0.0965 0.0154 0.1835 0.0811 0.3507
0.1455 0.1233 0.1839 0.1112 0.0430 0.1890 0.3063 0.3786
0.1818 0.1450 0.2400 0.1320 0.0598 0.2963 0.4359 0.5328
0.2599 0.2399 0.3377 0.2417 0.1690 0.3685 0.4468 0.5502
0.2638 0.3510 0.4173 0.3692 0.2348 0.4509 0.4868 0.6443
0.4314 0.4018 0.4893 0.3897 0.3532 0.5470 0.5085 0.7948
0.5797 0.5132 0.4909 0.4039 0.5752 0.6256 0.5108 0.8116
0.8001 0.5499 0.9001 0.7803 0.6491 0.6477 0.7757 0.8176
0.8693 0.6221 0.9027 0.9421 0.7317 0.6868 0.7802 0.8759
0.9106 0.8530 0.9448 0.9561 0.8212 0.7447 0.9294 0.9390
>> resul1=sort_mat(7:10,:)
resul1 =
0.5797 0.5132 0.4909 0.4039 0.5752 0.6256 0.5108 0.8116
0.8001 0.5499 0.9001 0.7803 0.6491 0.6477 0.7757 0.8176
0.8693 0.6221 0.9027 0.9421 0.7317 0.6868 0.7802 0.8759
0.9106 0.8530 0.9448 0.9561 0.8212 0.7447 0.9294 0.9390
>>
3 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!