Sort Function Help and Selecting Highest Values
이전 댓글 표시
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.
답변 (1개)
Walter Roberson
2011년 9월 23일
xsorted = sort(x(:));
y_largest_x = xsorted(end-y+1:end);
(Yes, that was deliberately written a bit clumsy.)
댓글 수: 12
Kyle
2011년 9월 23일
Kyle
2011년 9월 23일
Walter Roberson
2011년 9월 23일
hint: ismember
Kyle
2011년 9월 23일
Walter Roberson
2011년 9월 23일
You have determined the n highest values. Now, how are you going to test each value in x to determine whether it is one of those high values?
Kyle
2011년 9월 23일
Walter Roberson
2011년 9월 23일
So... read the documentation on the function named "ismember"
Kyle
2011년 9월 23일
Walter Roberson
2011년 9월 23일
Consider:
if ismember(SOMETHING(5),y_largest_x)
select(5) = x(5);
end
Now figure out what SOMETHING should be.
Kyle
2011년 9월 23일
Walter Roberson
2011년 9월 23일
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
2011년 9월 25일
This sounds like another challenge to see how few characters are needed ...
카테고리
도움말 센터 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!