How do I create this sorting function?
9 ビュー (過去 30 日間)
古いコメントを表示
Hi all. I am still a bit new to Matlab and I was asked to create a function with one input (X) and one output (Y) which ranks any set of numbers in such a way that for instance if X=[5 2 9 7], Y=[2 1 4 3]. This is to be done through the use of loops and logical operators. How would I go about doing this please? Thanks in advance.
0 件のコメント
回答 (1 件)
Dyuman Joshi
2022 年 5 月 3 日
編集済み: Dyuman Joshi
2022 年 5 月 4 日
For all unique values
input = [5 2 9 7];
output = sorting(input)
function y = sorting(x)
for i=1:numel(x)
y(i)=nnz(x(i)>=x); %you can use sum as well
end
end
3 件のコメント
Walter Roberson
2022 年 5 月 4 日
numel(x) is defined as the prod(size(x))
Walter Roberson
2022 年 5 月 4 日
編集済み: Walter Roberson
2022 年 5 月 4 日
Note: that sorting() function does not work if there are duplicate entries, and will return 0 for any entry that is NaN.
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!