Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I created the follwing function: %First for the vector we need to find the large

ID: 3589629 • Letter: I

Question

I created the follwing function:

%First for the vector we need to find the largest element the below is the %statement to sort the vector.

A=[1 2 5 0 5];

%sorting the vector.

[C,Index] = sort(A,'descend');

%taking the max value.

t = A(Index(1));

%finding the elements which are equal to max value.

idx = find(A==max(A),1,'first');

%using empty vector to deletes the largest elements.

A(idx) = [];

%printing the vector.

g = sprintf(' %d ',A);

fprintf(' Answer: %s ',g);


but the output I'm getting is 1 2 0 5 , it only removes one of the 5's when its supposed to remove both. How can I fix this ?

5.6 Write some statements on the command line to remove the largest element(s) from a vector. Try it out on x [1 2 5 0 5]·The idea is to end up with [1 2 0] in x. Use find and the empty vector

Explanation / Answer

A=[1 2 5 0 5];
%sorting the vector.
[C,Index] = sort(A,'descend');
%taking the max value.
t = A(Index(1));
%finding the elements which are equal to max value.
idx = find(A==max(A));
%using empty vector to deletes the largest elements.
A(idx) = [];
%printing the vector.
g = sprintf(' %d ',A);
fprintf(' Answer: %s ',g);

% basically you were searching only for first element rather than searching for all elements

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote