Sort an array logical like a stair

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

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?
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)

If you have Image Proc TBX, you can use
watershed
regionprops
on the logical array
isnan(A)

2 Comments

Sorry isn't it a bit to difficult for this problem?
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

For the sake of trying to understand your goal, what would be the outcome of this example:
A =
NaN 3 NaN 7 11
NaN NaN NaN 8 NaN
1 4 NaN NaN NaN
NaN 5 NaN 9 12
2 NaN 6 10 13
that's not how my matrix look like. This is how:
A =
NaN NaN NaN -1.9696 NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN -1.5494 NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN -1.6931 NaN NaN NaN NaN NaN NaN NaN
NaN -1.6998 NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
NaN -1.0770 NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN -0.8727 NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN -0.8195 NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN -0.4264 NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN -0.1408 NaN NaN NaN NaN NaN
NaN NaN NaN -0.1750 NaN NaN NaN NaN NaN NaN
NaN NaN NaN -0.0653 -0.0125 0.0235 0.0547 0.0880 0.1324 0.2131
NaN NaN NaN NaN NaN NaN NaN NaN 0.5361 0.3309
NaN NaN NaN NaN NaN NaN NaN 0.6435 NaN NaN
NaN NaN NaN NaN NaN NaN 0.6722 NaN NaN NaN
The index in your array for the values are just randomly chosen. In my array you can see the following path (at least a human) of the numbers. (if there are rows which contains only NaN, they can be deleted)
" 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.
"This is how:"
Good luck for you to find someone who can solve your human problem.

Sign in to comment.

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

Asked:

on 1 Oct 2018

Answered:

on 10 Oct 2018

Community Treasure Hunt

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

Start Hunting!