Sortrows alternative / matrix sorting

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

 Respuesta aceptada

Stephen23
Stephen23 el 13 de Dic. de 2018
Editada: Stephen23 el 13 de Dic. de 2018

1 voto

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

Más respuestas (1)

Bob Thompson
Bob Thompson el 12 de Dic. de 2018
Editada: Bob Thompson el 12 de Dic. de 2018

0 votos

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 comentarios

Hello Thank you for this. But when I try and use it i get:
time_sorted =
7 5 4
signal_sorted =
5 9 11
Im not sure how to fix this.
Thank you
Bob Thompson
Bob Thompson el 12 de Dic. de 2018
I'm not sure how you're getting that. I did catch one mistake I made with the if conditions, but my results with the test sample you gave came out as you indicate your results should.
Dan Page
Dan Page el 12 de Dic. de 2018
From sorted i get:
1 2 3 4 4 4 4
3 2 4 7 11 11 11
Bob Thompson
Bob Thompson el 13 de Dic. de 2018
Editada: Bob Thompson el 13 de Dic. de 2018
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 el 13 de Dic. de 2018
it works up to the last 2 colums where it has repeated 4 and 11.

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2017b

Etiquetas

Preguntada:

el 12 de Dic. de 2018

Editada:

el 13 de Dic. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by