我目前正在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
`
答案 0 :(得分:1)
[shape.cen]'
将为您提供以下数组:
ans =
28 136
122 136
344 391
548 493
548 191
344 191
122 391
现在它是一个常规数值数组,你可以使用sortrows
,就像这样。
map = sortrows([shape.cen]')
得到:
map =
28 136
122 136
122 391
344 191
344 391
548 191
548 493
如果你不想把它作为一个数值数组,而是一个类似于shape
的结构,你可以这样做:
[~, ID] = sortrows([shape.cen]')
map = shape(ID)'