2d arry sorting

조회 수: 5 (최근 30일)
Sahan Priyanga
Sahan Priyanga 2015년 11월 18일
댓글: Sahan Priyanga 2015년 11월 19일
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
Sahan Priyanga
Sahan Priyanga 2015년 11월 19일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Renato Agurto
Renato Agurto 2015년 11월 18일
편집: Renato Agurto 2015년 11월 18일
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

추가 답변 (1개)

Guillaume
Guillaume 2015년 11월 18일
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.

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by