フィルターのクリア

Sortrows alternative / matrix sorting

4 ビュー (過去 30 日間)
Dan Page
Dan Page 2018 年 12 月 12 日
編集済み: Stephen23 2018 年 12 月 13 日
I am trying to make a function to order a 2 row matrix by the top row so that the second row follows the first one. But i am unsure of how to do this. For the challenge i am trying to complete I am not allowed to use the in built sort function.
An example of this is:
INPUT
time = [ 1 3 2 4 7];
signal= [12 14 11 13 16];
Then the function would put this into a 2 row vector it would then be ordered and the output would be:
time_sorted = [ 1 2 3 4 7];
signal_sorted= [12 11 14 13 16];
This is my attempt at this but it isnt working.
Thank you
function [time_sorted,signal_sorted] = mysortdata(time,signal)
%make sure we have more than one element
if numel(time) <= 1
return
end
timeSignal = [time;signal];
%Picking the end of time to be a place where time is split in two
PivotTime = timeSignal(end);
% Removes this point from time
timeSignal(:,end) = [];
%create 4 arrays:
% LessTime/LessSignal: values in the array less than the pivot
% MoreTime/MoreSignal: values in the array greater than the pivot
LessTime = time(time <= PivotTime);
MoreTime = time(time > PivotTime);
LessSignal = time(time <= PivotTime);
MoreSignal = time(time > PivotTime);
% input Less and More into this function again
Less = mysortdata(LessTime,LessSignal);
More = mysortdata(MoreTime,MoreSignal);
%Put time back together again
timeSignal(1,:) = [Less, PivotTime, More];
time_sorted = timeSignal(1,:);
signal_sorted = timeSignal(2,:);
return
end

採用された回答

Stephen23
Stephen23 2018 年 12 月 13 日
編集済み: Stephen23 2018 年 12 月 13 日
function [A,B] = mysortdata(A,B)
M = qsRecFun([A;B]);
A = M(1,:);
B = M(2,:);
end
function M = qsRecFun(M)
N = size(M,2);
if N>1
X = fix(N/2);
S = M(:,[1:X-1,X+1:N]);
Y = S(1,:) < M(1,X);
L = qsRecFun(S(:, Y));
R = qsRecFun(S(:,~Y));
M = [L,M(:,X),R];
end
end
And tested:
>> T = [ 1, 3, 2, 4, 7];
>> S = [12,14,11,13,16];
>> [Ts,Ss] = mysortdata(T,S)
Ts =
1 2 3 4 7
Ss =
12 11 14 13 16
  1 件のコメント
Dan Page
Dan Page 2018 年 12 月 13 日
Thank you

サインインしてコメントする。

その他の回答 (1 件)

Bob Thompson
Bob Thompson 2018 年 12 月 12 日
編集済み: Bob Thompson 2018 年 12 月 12 日
I know it may not be the fastest method, but what about using a loop to sort each element. Because you have 'time' and a piece of corresponding data I'm assuming you will always have positive time values, so for each loop you can just pull out the column with the minimum time value into a new matrix, and then remove the column from the original, until you have the whole array sorted.
timesignal = [time;signal];
for i = 1:length(time);
[value,index] = min(timesignal(1,:));
sorted(:,i) = timesignal(:,index);
if index == 1
timesignal = timesignal(:,2:end);
elseif index == size(timesignal,2)
timesignal = timesignal(:,1:end);
else
timesignal = timesignal(:,[1:index-1,index+1:end]);
end
end
  5 件のコメント
Bob Thompson
Bob Thompson 2018 年 12 月 13 日
編集済み: Bob Thompson 2018 年 12 月 13 日
Are you using just the code I posted, or did you combine it with something else? That looks like what I would expect, for that particular set of numbers. Did you want them in a different order?
Dan Page
Dan Page 2018 年 12 月 13 日
it works up to the last 2 colums where it has repeated 4 and 11.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by