How can I create create a matrix with a pattern?

Hi,
I have a matrix S (4x12) like this:
S = [0 0 2 0 0 0 2 0 0 0 2 0;
0 0 0 2 0 0 0 2 0 0 0 2;
2 0 0 0 2 0 0 0 2 0 0 0;
0 2 0 0 0 2 0 0 0 2 0 0]
I need to create a matrix Z that is 4x27 and contains the original pattern from S in its first 12 columns and continues the pattern up to column 27.
Any help would be greatly appreciated.

 Accepted Answer

Try this:
z = repmat(S, [1, 3]); % Replicate S
z =z(:, 1:27) % Extract only the 27 columns that are needed.

3 Comments

Thanks for the answer! This is great!
What exactly does the "[1, 3]" in repmat do?
It tells repmat() how many copies to make in the rows (vertical) direction and columns (horizontal) direction. So it takes S and copies it once in the vertical direction (not an additional copy, just the one original matrix), and makes 3 copies in the horizontal direction. So in the end you have 3 copies side-by-side. Now since S was 12 wide to start, you'll end up with a 36 column wide matrix. That's why I had to crop off anything beyond 27 columns which is all you wanted.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!