Creating a (nxn) Matrix using a for loop

How do i create a (n x n) matrix whose elements are either +3 or –3 such that the sign of each element is different from its adjacent elements, both from those above and below it and from those on either side of it (see Y below). I want to do this using a for loop.

3 件のコメント

Andrew Newell
Andrew Newell 2011 年 4 月 11 日
You say "see Y below" but there's no Y.
Andrew Newell
Andrew Newell 2011 年 4 月 11 日
This is a duplicate post. What's wrong with the answers to the previous question? I suggest you delete this question and go back there.
Walter Roberson
Walter Roberson 2011 年 5 月 13 日
duplicates http://www.mathworks.com/matlabcentral/answers/5202-how-to-create-a-nxn-matrix-with-alternating-sign

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

回答 (1 件)

the cyclist
the cyclist 2011 年 4 月 11 日

0 投票

Here's one way, without a for loop:
A = 3*reshape((-ones(1,n^2)).^(1:n^2),[n n])
Force a for loop into that somewhere.

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

質問済み:

2011 年 4 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by