Clear Filters
Clear Filters

Sort an array logical like a stair

2 views (last 30 days)
Benutzer Name
Benutzer Name on 1 Oct 2018
Answered: Benutzer Name on 10 Oct 2018

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 Comments
Jan
Jan on 1 Oct 2018
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 on 1 Oct 2018
sorry I made a mistake. it should go from [6,11] to [7,11] and follow with [7,1].

Sign in to comment.

Answers (3)

Bruno Luong
Bruno Luong on 1 Oct 2018
If you have Image Proc TBX, you can use
watershed
regionprops
on the logical array
isnan(A)
  2 Comments
Benutzer Name
Benutzer Name on 1 Oct 2018
Sorry isn't it a bit to difficult for this problem?
Bruno Luong
Bruno Luong on 1 Oct 2018
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.

Sign in to comment.


Benutzer Name
Benutzer Name on 2 Oct 2018
Nobody who can help me with a short solution?
  4 Comments
Stephen23
Stephen23 on 2 Oct 2018
" 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 on 2 Oct 2018
"This is how:"
Good luck for you to find someone who can solve your human problem.

Sign in to comment.


Benutzer Name
Benutzer Name on 10 Oct 2018
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)];

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!