Delete column from matrix with condition?

1 view (last 30 days)
Hi,
I am coding the following code:
ranking = [1 4 2 3];
Y = [ 1 2 3 4; 6 5 4 3; 1 2 3 4];
r=1;
n=2;
k=length(ranking);
for i=1:k
while r<n
if (ranking(i)== min(ranking))
Z(:,i)=Y(:,i);
ranking(i)=max (ranking);
r=r+1;
else
Z(:,i)=[];
end
break
end
end
display(Z)
ranking
My code aims to create a new matrix "Z" only containing columns with highest ranking from matrix "Y"(ranking(1) and ranking(2) are supposed as most important as we chose "n=2"). So the final matrix "Z" must look like this:
Z=[1 3; 6 4; 1 3];
When i run my code i am not getting this result instead i got hte following output:
Z= [1 3 3; 6 4 4; 1 3 3];
I am missing something wrong in my code.
Any help please?

Answers (1)

TADA
TADA on 10 Nov 2018
Z = Y(:,ranking <= n);

Community Treasure Hunt

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

Start Hunting!