Delete column from matrix with condition?

1 vue (au cours des 30 derniers jours)
Thouraya Aouled Messaoud
Thouraya Aouled Messaoud le 10 Nov 2018
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?

Réponses (1)

TADA
TADA le 10 Nov 2018
Z = Y(:,ranking <= n);
  1 commentaire
Thouraya Aouled Messaoud
Thouraya Aouled Messaoud le 10 Nov 2018
This worked ;) no need for so much code. Thanks !!!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by