Delete column from matrix with condition?

1 次查看(过去 30 天)
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
Z = Y(:,ranking <= n);

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by