필터 지우기
필터 지우기

Sort an array logical like a stair

조회 수: 3 (최근 30일)
Benutzer Name
Benutzer Name 2018년 10월 1일
답변: Benutzer Name 2018년 10월 10일

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