Finding the indices of duplicate maximum number of non zero element in matrix
1 view (last 30 days)
Show older comments
I have this matrix
A=[-1 -0.3313 0 1 0.55 -0.2186 -1.0041 -0.0297 0 0.0041 0 0.0297;
0 0 0 0 1.09 -0.09 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0.5 0.5;
0 0 0 0 0 0 0 0 0 0 -0.375 1.375;
-1 0.2108 0 1 -0.35 0.1392 -0.9675 0.238 0 -0.0325 0 -0.238];
I want to find a rows that gives me maximum number of non zero element.
answer should be row=[1,5]
I use this one [~,Q1]=max(sum(A~=0,2) ) but it does not give [1,5]
0 Comments
Accepted Answer
madhan ravi
on 7 Jan 2019
Edited: madhan ravi
on 7 Jan 2019
EDITED
Note: max only returns the index of the max value at the first instant so in order to find all the indices of max value you would have to use find().
A=[-1 -0.3313 0 1 0.55 -0.2186 -1.0041 -0.0297 0 0.0041 0 0.0297;
0 0 0 0 1.09 -0.09 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0.5 0.5;
0 0 0 0 0 0 0 0 0 0 -0.375 1.375;
-1 0.2108 0 1 -0.35 0.1392 -0.9675 0.238 0 -0.0325 0 -0.238];
a=(sum(A~=0,2));
Row=find(a==max(a)).'
Gives:
Row =
1 5
1 Comment
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices 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!