create coordination matrix from zig zag path

I want to creat matrix with x and y coordiantion of zig zag path.
For example :
So the generated matrix should be like this:
A = [ o o
0 410
1 410
1 0
2 0
2 410
3 410
3 0
4 0
4 410
. ... ......]
let's say the x direction limit is 8 . How can we do this ?

 採用された回答

David Hill
David Hill 2020 年 7 月 14 日

0 投票

x=0:9;%xend odd only
y=[0;410;410;0];
A=[repelem(x',2),repmat(y,(x(end)+1)/2,1)];

6 件のコメント

Islam Hassan
Islam Hassan 2020 年 7 月 14 日
Thanks a lot, but why the x limit can be only odd, is there nay way to make it odd or even?
David Hill
David Hill 2020 年 7 月 14 日
x=0:9;%xend odd only
y=[0;410;410;0];
A=[repelem(x',2),repmat(y,(x(end)+1)/2,1)];
A=A(1:2*(x(end)-1),:);%just eliminate last two rows for even
Islam Hassan
Islam Hassan 2020 年 7 月 14 日
The idea that I will link this code to another code, and I do not know whether the number will be odd or even. Thus, I want somthing to work without depending on the type of number (odd or even).
David Hill
David Hill 2020 年 7 月 14 日
x=0:9;
y=[0;410;410;0];
if mod(x(end),2)==1
A=[repelem(x',2),repmat(y,(x(end)+1)/2,1)];
else
X=[x,x(end)+1];
A=[repelem(X',2),repmat(y,(X(end)+1)/2,1)];
A=A(1:2*X(end),:);
end
Islam Hassan
Islam Hassan 2020 年 7 月 14 日
Thanks a lot that works
Islam Hassan
Islam Hassan 2020 年 7 月 14 日
Sorry, I tried the below code you mentioned, however, it end with (9 0), however I want to end at (8 0).
So, for example if the x limit is 8 I want the generated matix to stop at ( 8 0), or if the x limit is 9, the matrix stop at (9 0)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by