Sort an array logical like a stair

2 次查看(过去 30 天)

Hello everyone,

I got a simple problem and I thought maybe there is already a function in MATLAB I could use. I got an array like in the picture and I want to sort the values like the route from the red line.

At the end I want a 1xN array.

Do somebody know how to sort them easily like this ?

Thanks!

<<

>>

  2 个评论
Jan
Jan 2018-10-1
There are 3 red lines, therefore it is not uniquely clear, what you want as output. Is it wanted, that on the right middle part, the 0.6286 is not included, but the NaN above is?
Benutzer Name
Benutzer Name 2018-10-1
sorry I made a mistake. it should go from [6,11] to [7,11] and follow with [7,1].

请先登录,再进行评论。

回答(3 个)

Bruno Luong
Bruno Luong 2018-10-1
If you have Image Proc TBX, you can use
watershed
regionprops
on the logical array
isnan(A)
  2 个评论
Benutzer Name
Benutzer Name 2018-10-1
Sorry isn't it a bit to difficult for this problem?
Bruno Luong
Bruno Luong 2018-10-1
No, just don't quite understand how you can get a stair boundaries in general, and why there is no branch. So I give you a generic tool that handle all situations.

请先登录,再进行评论。


Benutzer Name
Benutzer Name 2018-10-2
Nobody who can help me with a short solution?
  4 个评论
Stephen23
Stephen23 2018-10-2
" In my array you can see the following path (at least a human)"
But we need an algorithm, if you want this written in any kind of code.
Bruno Luong
Bruno Luong 2018-10-2
"This is how:"
Good luck for you to find someone who can solve your human problem.

请先登录,再进行评论。


Benutzer Name
Benutzer Name 2018-10-10
If someone is still interested in my human problems, here is my code that works: (Note: only if you set the start position right, here at: (1,1))
ii=1;jj=1;
%running index
nn=1;
%create a M-by-N Matrix with zeros for x and y to do a condition in the
%following while loops
XX=zeros(size(xx,1),size(xx,2)); YY=zeros(size(yy,1),size(yy,2));
%search for the "right" way from top to bottom
while 1
while 1
if xx(ii,jj)~=0
XX(ii,nn)=xx(ii,jj);
YY(ii,nn)=yy(ii,jj);
nn=nn+1;
end
%special case: if you looking at the last column
if jj>=size(xx,2) && xx(ii,1)~=0
xx(ii,jj)=0;
jj=1;
elseif jj>=size(xx,2) && xx(ii,size(xx,2)-1)~=0
xx(ii,jj)=0;
jj=jj-1;
elseif jj>=size(xx,2)
if xx(ii,size(xx,2)-1)==0 && xx(ii,1)==0
xx(ii,jj)=0;
break
end
%special case: if you looking at the first column
elseif jj<=1 && xx(ii,2)~=0
xx(ii,jj)=0;
jj=jj+1;
elseif jj<=1 && xx(ii,size(xx,2))~=0
xx(ii,jj)=0;
jj=size(xx,2);
elseif jj<=1
if xx(ii,jj+1)==0 && xx(ii,size(xx,2))==0
xx(ii,jj)=0;
break
end
%normal case
elseif xx(ii,jj+1)~=0
xx(ii,jj)=0;
jj=jj+1;
elseif xx(ii,jj-1)~=0
xx(ii,jj)=0;
jj=jj-1;
elseif xx(ii,jj+1)==0 && xx(ii,jj-1)==0
xx(ii,jj)=0;
break
end
end
%start at the first line in new row
nn=1;
%new row
ii=ii+1;
%break main while loop if your are done with the last row
if ii>size(xx,1)
break
end
end
%rotate for sort
x2=XX'; y2=YY';
%delete all zeros
x2(x2==0)=[]; y2(y2==0)=[];
%close array
x=[x2 x2(1)]; y=[y2 y2(1)];

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by