pseudo code for all n values

2 ビュー (過去 30 日間)
Elif Kalender
Elif Kalender 2021 年 4 月 4 日
コメント済み: Elif Kalender 2021 年 4 月 4 日
n=input('n= ');
A=ones(n);
A(2:n,2)=0;
A(2,2:n)=0;
A(4:n,4)=0;
A(4,4:n)=0;
A(6:n,6)=0;
A(6,6:n)=0
I wrote that code for n=7 but I want this to be true for every n value, how can i convert the code?

回答 (2 件)

Walter Roberson
Walter Roberson 2021 年 4 月 4 日
n=input('n= ');
A=ones(n);
A(2:n,2)=0;
A(2,2:n)=0;
A(4:n,4)=0;
A(4,4:n)=0;
A(n-1:n,n-1)=0;
A(n-1,n-1:n)=0;
Possibly 4 is ceil(n/2) but if so you need to decide what you want to do if n is even.
  1 件のコメント
Elif Kalender
Elif Kalender 2021 年 4 月 4 日
Thank you so much.I will look at it with that perspective.

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


the cyclist
the cyclist 2021 年 4 月 4 日
Assuming that what you wanted was alternating pattern of 1's and 0's, regardless of how large n is, then this should do what you want:
n=input('n= ');
A=ones(n);
for ii = 2:2:n
A(ii:n,ii)=0;
A(ii,ii:n)=0;
end
  1 件のコメント
Elif Kalender
Elif Kalender 2021 年 4 月 4 日
It really works very well!Thank you so much!

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

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by