3

I have a 4x1 matrix,

A= [1;2;3;4]   

I want to make B such that its size is 4x50. All the elements in the columns must contain the same elements of A. For example,

B= [1 1 1 1.... 1 1; 2 2 2 2.... 2 2; 3 3 3 3.... 3 3; 4 4 4 4.... 4 4]

In this case all the elements of A from column 1 is present in the same way in the first column of B, same for second column on B, and so on

Is there any way to form B like this from A? I was trying concatenating like below:

B= horzcat(A,A,...);

But in this case, I have to write A, 50 times. So is there any other way to get the same result?

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Chris33
  • 103
  • 2
  • 8

1 Answers1

4

Have you tried using repmat?

B = repmat(A, 1, 50);

repmat (which nicely stands for repeat matrix) takes a matrix and repeats itself for as many times horizontally and vertically that you want. Technically speaking, you can choose how many times you want to repeat for as many dimensions as possible as there are in your matrix. However, for our purposes here, this is a matrix which has two degrees of freedom / dimensions, so we're only considering horizontal and vertical here.

In your specific case, you want to repeat this column vector 50 times horizontally, hence the third parameter being set to 50, while you only want one copy vertically, hence the second parameter being set to 1.

rayryeng
  • 102,964
  • 22
  • 184
  • 193