Suggestion on what im doing wrong.

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
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
Sudhakar Shinde 2020년 10월 22일

0 개 추천

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

Rik
Rik 2020년 10월 22일
Why did you delete and repost the exact same answer? Now all the comments are gone. I think that is a bit rude. I still think you shouldn't post a complete solution to a homework question. You might not agree, but I don't see why that would be a reason to remove all comments. People can easily ignore comments if they like to.
If you remove this thread as well I will copy the comments from the activity feed and put them in a comment under the question so you will not be able to delete it.
Osman Motta
Osman Motta 2020년 10월 22일
Thank you for the input. When I posted the original I made grammatical errors due to the fact I’ve been awake for 40hr due to an emergency, I would never delete someone’s input whether I agreed with it or not. I appreciate your help.
Rik
Rik 2020년 10월 22일
@Osman, no problem, my comment was not aimed at you.

이 질문은 마감되었습니다.

질문:

2020년 10월 22일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by