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.

 採用された回答

Image Analyst
Image Analyst 2017 年 4 月 2 日

0 投票

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

3 件のコメント

John Smith
John Smith 2017 年 4 月 2 日
Thanks for the answer! This is great!
John Smith
John Smith 2017 年 4 月 2 日
What exactly does the "[1, 3]" in repmat do?
Image Analyst
Image Analyst 2017 年 4 月 2 日
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.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by