2014-01-31 3 views
1

현재 matlab에서 맵 처리를하고 있습니다. 이제 미로를 해결하고 미로의 길을 잡았습니다. 이제 나는지도에서 전환점을 얻었습니다. 그러나이 주소 픽셀은 올바른 순서가 아닙니다. 그래서 올바른 순서로 픽셀 주소의 잘못된 순서를 주문하고 싶습니다.특정 조건에서 픽셀 주소를 정렬하는 방법은 무엇입니까?

잘못된 순서 :

shape(1).cen=[28;136]; 
shape(2).cen=[122;136]; 
shape(3).cen=[344;391]; 
shape(4).cen=[548;493]; 
shape(5).cen=[548;191]; 
shape(6).cen=[344;191]; 
shape(7).cen=[122;391]; 

올바른 ORDR :

map(1).cen=[28;136]; 
map(2).cen=[122;136]; 
map(3).cen=[122;391]; 
map(4).cen=[344;391]; 
map(5).cen=[344;191]; 
map(6).cen=[548;191]; 
map(7).cen=[548;493]; 

내 코드는 다음과 같습니다 : -

`map(1).cen=[28;136]; 
o=0; order=1;xflag=0;yflag=0; 
k=length(shape); %indicates the total elements in shape.cen structure 
for (j=1:k) 
order=order+1; o=o+1; 
if (j==1) 
    x=map(1).cen(1,1); 
    y=map(1).cen(2,1); 
    for(i=1:k) 
     xi=shape(i).cen(1,1); 
     yi=shape(i).cen(2,1); 
     if((x==xi)||(y==yi)) 
      if(x==xi) 
       map(order).cen(1,1)=xi; 
       map(order).cen(2,1)=yi; 
       xflag=1; 
       break; 
      else 
       (y==yi) 
       map(order).cen(1,1)=xi; 
       map(order).cen(2,1)=yi; 
       yflag=1; 
       break; 
      end 
     end 
    end 
end 

x=map(o).cen(1,1); 
y=map(o).cen(2,1); 

for(i=1:k) 
    xi=shape(i).cen(1,1); 
    yi=shape(i).cen(2,1); 
    if(xflag==1) 
     if(y==yi) 
      map(order).cen(1,1)=xi; 
      map(order).cen(2,1)=yi; 
      xflag=0; 
      yflag=1; 
      break; 
     end 
    end 

    if (yflag==1) 
     if(x==xi) 
      map(order).cen(1,1)=xi; 
      map(order).cen(2,1)=yi; 
      xflag=1; 
      yflag=0; 
      break; 
     end 
    end 
end 
end 

`

+1

당신에 대해 말하는 경우 '주문'보다 어떻게 든 정렬해야합니다. 당신은 무엇을 정렬합니까? 이 예제에서 첫 번째 좌표의 값을 기준으로 정렬 순서를 변경하는 것 같습니다. – Marcin

+1

체크 아웃 [sortrows] (http://www.mathworks.com/help/matlab/ref/sortrows.html) –

답변

1

[shape.cen]'는 당신에게 따라와 줄 것이다 g 배열 : 지금은 일반 숫자 배열 있다는

ans = 
    28 136 
    122 136 
    344 391 
    548 493 
    548 191 
    344 191 
    122 391 

, 당신은 다음과 같이 sortrows를 사용할 수 있습니다.

map = sortrows([shape.cen]') 

는 얻을 : 당신은 숫자 배열,하지만 shape 유사한 구조체로하지 않으려면

map = 
    28 136 
    122 136 
    122 391 
    344 191 
    344 391 
    548 191 
    548 493 

, 당신이 할 수 있습니다

[~, ID] = sortrows([shape.cen]') 
map = shape(ID)' 
관련 문제