Delete column from matrix with condition?

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?

回答 (1 件)

TADA
TADA 2018 年 11 月 10 日

1 投票

Z = Y(:,ranking <= n);

1 件のコメント

Thouraya Aouled Messaoud
Thouraya Aouled Messaoud 2018 年 11 月 10 日
This worked ;) no need for so much code. Thanks !!!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by