Sort Function Help and Selecting Highest Values

2 Ansichten (letzte 30 Tage)
Kyle
Kyle am 23 Sep. 2011
Hello, I am trying to create a MatLab function that takes in two inputs, x and y, and outputs select. select should be the same size as x. The elements of select should be the same as the corresponding element x if that element is one of the y largest values in x and 0 otherwise.
I know I am supposed to be using the Sort function to help pick the highest values, but I'm pretty unsure how to make this function.
Thank you for any help you can give.

Antworten (1)

Walter Roberson
Walter Roberson am 23 Sep. 2011
xsorted = sort(x(:));
y_largest_x = xsorted(end-y+1:end);
(Yes, that was deliberately written a bit clumsy.)
  12 Kommentare
Walter Roberson
Walter Roberson am 23 Sep. 2011
No.
It is difficult to give any more hints without giving the key answer away, which would ruin the purpose of the assignment.
The entire assignment can be completed with two statements plus the function header. Ah yes... I just remembered the hack that would allow it to be written as a single statement plus the function header.
Daniel Shub
Daniel Shub am 25 Sep. 2011
This sounds like another challenge to see how few characters are needed ...

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by