maximizing efficiency in simple commands

1 view (last 30 days)
I have an array given by
x = [1 2 3 4];
I would like to create a matrix that looks like
M = [1 2 3 4;
1 2 3 4;
1 2 3 4;
1 2 3 4];
What's the most computationally efficient way to create M from x?

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 20 Aug 2013
Edited: Azzi Abdelmalek on 20 Aug 2013
x=[1 2 3 4];
M=repmat(x,4,1)
  1 Comment
Walter Roberson
Walter Roberson on 20 Aug 2013
Equivalently but removing the error checking:
x=[1 2 3 4];
M = x([1 1 1 1],:);

Sign in to comment.

More Answers (1)

Andrei Bobrov
Andrei Bobrov on 20 Aug 2013
Edited: Andrei Bobrov on 20 Aug 2013
ones(4,1)*x
kron(ones(4,1),x)

Community Treasure Hunt

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

Start Hunting!