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.

 Respuesta aceptada

Image Analyst
Image Analyst el 2 de Abr. de 2017
Try this:
z = repmat(S, [1, 3]); % Replicate S
z =z(:, 1:27) % Extract only the 27 columns that are needed.

3 comentarios

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 2 de Abr. de 2017

Comentada:

el 2 de Abr. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by