2d arry sorting

6 views (last 30 days)
Sahan Priyanga
Sahan Priyanga on 18 Nov 2015
Commented: Sahan Priyanga on 19 Nov 2015
i have 2d array which consist of
  1. ). 1st row for y axis coordinate of point(i)
  2. ). 2nd row for x axis coordinate of point(i)
  3. ). ilet consider following
a(1,:)=[1,2,3,4,10,11,12,13,19,20,21,22];
a(2,:)=[4,1,3,2,4,3,1,2,3,2,4,1];
a(3,:)=[1,2,3,4,5,6,7,8,9,10,11,12];
according to the above array 'a' it shows that it is shorted according to the first column(according to y coordinates).
but in my case i want to sort them with following steps
  • identify the position where the difference between two consecutive values of y coordinates (values of a(a,:)) changes rapidly and
  • sort the values between those rapidly change with respect to x coordination (a(2,:))
let consider the following
a =
1 2 3 4 10 11 12 13 19 20 21 22
4 1 3 2 4 3 1 2 3 2 4 1
1 2 3 4 5 6 7 8 9 10 11 12
0 1 1 1 6 1 1 1 6 1 1 1
here 4th row represent the difference between two consecutive y coordinates (a(1,:))
in there 5th value shows rapid change so i want consider first 4 value set and analyze the x and y coordinates w.r.t x coordinate (a(2,:))
in same way for whole array and following array represented the expected results.
a =
2 4 3 1 12 13 11 10 22 20 19 21
1 2 3 4 1 2 3 4 1 2 3 4
1 2 3 4 5 6 7 8 9 10 11 12
0 1 1 1 6 1 1 1 6 1 1 1
note by:
the 3rd column value should not be changed.
the rapid changed is not obtained in same period like in above it varying.(in my case it happened after every 4 values )
the rapid change is not 6 for every instant (let the solution should convenience for values greater than 3)
  • can any one help me to code this*
  1 Comment
Sahan Priyanga
Sahan Priyanga on 19 Nov 2015
thanks for your quick response, it is worked correctly.
but i'm stuck in following situation. I have think i can manage it but it is really difficult to manage in the case when those are stored in structured cell array.
in my case both x and y coordinates are in 'Centroid' field and many other columns/field also there(s.t. boundingbox,fixelvalue which are related to certain images).
the simply meaning is that when sorting i want to move they also without changing their original position.

Sign in to comment.

Accepted Answer

Renato Agurto
Renato Agurto on 18 Nov 2015
Edited: Renato Agurto on 18 Nov 2015
Hi,
is this what you want?
Cheers!
Renato
clear
a(1,:)=[1,2,3,4,10,11,12,13,19,20,21,22];
a(2,:)=[4,1,3,2,4,3,1,2,3,2,4,1];
a(3,:)=[1,2,3,4,5,6,7,8,9,10,11,12];
N = size(a,2);
%Create 4th row
a(4,1) = 0;
a(4,2:end) = a(1,2:end) - a(1,1:end-1);
delta = 3;
i1 = 1;
for i = 1:N
%find range to sort i1:i2
if i == N || a(4,i+1) > delta
i2 = i;
%extract the part of the 2d-array to be sorted
tmp_a = a(1:2,i1:i2);
[~, I] = sort(tmp_a(2,:));
%put it back in the 2d-array
a(1:2,i1:i2) = tmp_a(1:2, I);
i1 = i+1;
end
end

More Answers (1)

Guillaume
Guillaume on 18 Nov 2015
Here's how I'd do it:
a = [1 2 3 4 10 11 12 13 19 20 21 22
4 1 3 2 4 3 1 2 3 2 4 1
1 2 3 4 5 6 7 8 9 10 11 12];
splitlength = diff([0 find(diff(a(1, :)) >= 3) size(a,2)]); %find distance between rapid changes
splita = mat2cell(a([1 2], :), 2, splitlength); %split 1st two rows of into sub matrices
sortedsplita = cellfun(@(m) sortrows(m', 2)', splita, 'UniformOutput', false); %sort submatrices by second row
sorteda = [cell2mat(sortedsplita); a(3, :)] %recombine submatrices and 3rd row of a
Vectorised code, so should be fast.

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!