0

Hi guys, I'm taking an engineering class and i'm not familiar with Matlab at all. I've been trying to generate a square matrix from an input with a pattern.

For example:
1 0
0 1

1 0 1
0 1 0
1 0 1

1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1

I started with a = input('enter the size')
I tried making all zeros and then trying to modify the odds or make all 1's and try to replace some of them with 0s. Could someone point me towards the function or commands i should use? thank you very much!

3
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by hardmath
0

Try "eye" - http://www.mathworks.com/access/helpdesk/help/techdoc/ref/eye.html .

Hi guys, I'm taking an engineering class and i'm not familiar with Matlab at all. I've been trying to generate a square matrix from an input with a pattern.

For example:
1 0
0 1

1 0 1
0 1 0
1 0 1

1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1

I started with a = input('enter the size')
I tried making all zeros and then trying to modify the odds or make all 1's and try to replace some of them with 0s. Could someone point me towards the function or commands i should use? thank you very much!

0

While "eye" will give a square matrix (the identity matrix I), I suspect that the OP wants a way to obtain a square matrix whose entries alternate between 1 and 0, a "checkerboard" pattern. This can be done for even dimensions by "replicating" the identity matrix using call repmat(eye(2),n,n) to get a 2n-by-2n checkerboard pattern. See the example here:

Replicate and tile array -- MATLAB

For odd dimensions it may be simplest to create the next larger even size matrix and delete the last row and column. Suppose k = 2n and a (k-1)-by-(k-1) matrix is needed:

X = repmat(eye(2),n,n);
X(:,k) = [];
X(k,:) = [];

regards, hm

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.