Cannot find error in my sort function
显示 更早的评论
So my code is supposed to work so for an input array, findSortedMatches sorts the array and looks for rows that didn’t move. It returns the number of rows that didn’t move (matches) and an array of the rows that didn’t move (matched rows).
Code
function [matches, matched_rows] = findSortedMatches(score)
score = [N,2];
matches = 0;
matched_rows = [];
[~, sorted] = sortdata(score);
next_unsorted = make_nextRow(score,1);
next_sorted = make_nextRow(sortdata,1);
for i = 1:size(score,2)
unsorted = next_unsorted;
if all(unsorted==next_sorted)
next_sorted() matches unsorted
matches = matches + 1;
matched_rows = [matched_rows; unsorted];
end
end
end
function [data_sortbyscore, bestscore_id] = sortdata( score )
[~, idx] = sort(score(:, 2));
data_sortbyscore = score(idx, :);
bestscore_id = data_sortbyscore(end, 1);
end
Test Case and Expected Answers
>> scores = [20,90;13,56;3,67;10,78;2,54];
>> [num_matches, row_matches] = findSortedMatches(scores)
num_matches =
3
row_matches =
13 56
3 67
10 78
>>
>>
>> array = [2 34; 5 67];
>> [num_matches, matched_rows]=findSortedMatches(array)
num_matches =
2
matched_rows =
2 34
5 67
>> array = [5 67; 2 34];
>> [num_matches, matched_rows]=findSortedMatches(array)
num_matches =
0
matched_rows =
[]
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!