필터 지우기
필터 지우기

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개)

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 CenterFile Exchange에서 Shifting 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