Effacer les filtres
Effacer les filtres

in a matrix (a val output from sort): sort first column based on whether the numbers in the second column are the same

4 vues (au cours des 30 derniers jours)
Here is what I have:
[Pos, num] = MyFun(handles);
%Pos is a i x 2 (the # of rows changes size every session) (row = x and y coords)
num is a i x 1 (row also changes)
[val, idx] =sort(num,1,'descend');
for i = 1:size(Pos,1)
new_Pos(i,:) = Pos((idx(i,1)),:);
end
So, as you can see, I have taken my output, sorted it based on value, used the index of the sort function to rearrange my Pos matrix so that it is descending based on the number associated with it (the number is the output from MyFun).
Now, what I need to do is, in addition to sorting Pos by the number associated with it, I also need to sort it by the distance between,for example, Pos(1,:) and Pos(2,:) and so on until Pos(end-1,:) and Pos(end,:). The distance only matters for Positions that have the same 'num' value. So, I need to sort a matrix that changes each session. I only need to sort the first columns of rows that have the same number for their second column: for example: a matrix of column 1 = dist between positions and the column 2 = num
1 4
3 4
1 4
2 5
3 5
1 5
%So I need to sort that so it looks like this:
1 4
1 4
3 4
1 5
2 5
3 5
I have attempted to do this but I am stumped ...
val_dist = [];
k = size(val,1);
for i = 1:(k-1)
if val(i,1) == val(i+1,1)
val_dist = [sqrt([PosXYZ(idx(i,1),1) - PosXYZ(idx(i+1,1),1)]^2 + [PosXYZ(idx(i,1),2) - PosXYZ(idx(i+1,1),2)]^2), val(i,1); val_dist ];
end
end
n=[];
n_save = [];
for i = 1: (size(val_dist)-1)
while val_dist(i,2) == val_dist(i+1,2)
n = [n; val_dist(i+1,1)];
[n_val n_idx] = sort(n,1);
n_save = [n_save; n_val, n_idx];
end
end
  1 commentaire
Brittany
Brittany le 23 Juin 2011
I'm sorry, I forgot to update my second code to the more general form i started in the first code.
PosXYZ is the same thing as Pos

Connectez-vous pour commenter.

Réponse acceptée

Sean de Wolski
Sean de Wolski le 23 Juin 2011
sortrows(pos, [2 1])

Plus de réponses (0)

Catégories

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

Community Treasure Hunt

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

Start Hunting!

Translated by