Effacer les filtres
Effacer les filtres

Sortrows alternative / matrix sorting

4 vues (au cours des 30 derniers jours)
Dan Page
Dan Page le 12 Déc 2018
Modifié(e) : Stephen23 le 13 Déc 2018
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

Réponse acceptée

Stephen23
Stephen23 le 13 Déc 2018
Modifié(e) : Stephen23 le 13 Déc 2018
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

Plus de réponses (1)

Bob Thompson
Bob Thompson le 12 Déc 2018
Modifié(e) : Bob Thompson le 12 Déc 2018
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 commentaires
Bob Thompson
Bob Thompson le 13 Déc 2018
Modifié(e) : Bob Thompson le 13 Déc 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 le 13 Déc 2018
it works up to the last 2 colums where it has repeated 4 and 11.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by