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!

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!

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:

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.