Info
此问题已关闭。 请重新打开它进行编辑或回答。
Suggestion on what im doing wrong.
1 次查看(过去 30 天)
显示 更早的评论
I need to write a code that exclude the 2 smallest elements
clear all;clc
A=[8 1 -2 9 5 11 6]; %Should print A=[8 9 5 11 6]
B=[];
C=[];
for i=1:length(A)
if A(i)==min(A)
else
B=[B A(i)];
if A(i)==min(A)
else
C=[C A(i)];
end
end
end
C
This is a homework question and I am not looking for someone to do the work for me. I just need to know what I'm doing wrong.
So I just came up with this and I'm testing multiple numbers. Does it make sense and is there a way to simplify it?
clear all;clc
A=[8 1 -2 9 5 6];
B=[];
C=[];
for i=1:length(A)
if A(i)==min(A)
else
B=[B A(i)];
end
end
for i=1:length(B)
if B(i)==min(B)
else
C=[C B(i)];
end
end
C
So far it seems to work but I would appreciate any constructive input.
2 个评论
Walter Roberson
2020-10-22
What is the expected result if the vector contains duplicate values that are equal to the minimum? What is the expected result if the vector contains duplicate values that are equal to the second-smallest original value?
Can we assume that the values are non NaN (Not A Number)?
回答(1 个)
Sudhakar Shinde
2020-10-22
The 'min' function and for loop will help:
A=[8 1 -2 9 5 11 6];
n=2; % Exclude 2 smallest elements
for i=1:n
[num,id]=min(A);
A(id)='';
end
disp(A)
3 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!