Info

This question is closed. Reopen it to edit or answer.

could anyone help me to solve the error in the following code

1 view (last 30 days)
I am having the following matrix
A=[0.2288 0.2193 0.2082 0.1961 0.1832 0.1699;
1.1237 1.0564 0.9837 0.9092 0.8392 0.7826;
0.0451 0.0401 0.0331 0.0245 0.0152 0.0092;
0.8840 0.9053 0.9243 0.9396 0.9501 0.9548;
0.1320 0.1442 0.1545 0.1628 0.1689 0.1731]
I want to find two maximum values in each column
so i tried with the following code:
max_values = 2;
[v1,idx1]=sort(A,1,'descend');
W1 = zeros(size(A));
W1(sub2ind(size(A),repmat((1:size(A,2)).',...
1,2),idx(:,1:max_values))) = v1(:,1:max_values)
when i run the code i am getting error stating Error using sub2ind The subscript vectors must all be of the same size.
Could anyone please help me on this.
  6 Comments
Alex Mcaulley
Alex Mcaulley on 1 Aug 2019
I totally agree Madhan XD. Maybe it is useful for other readers

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 1 Aug 2019
Edited: KALYAN ACHARJYA on 1 Aug 2019
"I want to display first two maximum vaues in each column"
out=sort(A(:,:),'descend');
data=out(1:2,:)
Result:
data =
1.1237 1.0564 0.9837 0.9396 0.9501 0.9548
0.8840 0.9053 0.9243 0.9092 0.8392 0.7826
  9 Comments

Tags

Products


Release

R2015a

Community Treasure Hunt

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

Start Hunting!