-1

I have a matrix A:

A = [-10 10];

And I'd like to create B like this:

[ -10 10
  -10 10
  -10 10
  -10 10
  .
  .
  -10 10 ]

with d rows.

I tried:

B(1:d)=A(:);

to fill rows from 1 to d with A and a few other combinations, but I can't make it work. How can I achive this?

alex
  • 10,900
  • 15
  • 70
  • 100
  • 3
    use the [`repmat`](http://www.mathworks.com/help/matlab/ref/repmat.html) function – Dan Aug 12 '14 at 12:09
  • 2
    Also, consider skipping creating `B`. Depending on what you want to do with `B`, you may find you can do it with the original `A`, which is smaller. [`bsxfun`](http://www.mathworks.es/es/help/matlab/ref/bsxfun.html) may be handy – Luis Mendo Aug 12 '14 at 12:11

1 Answers1

1

Many options, the simplest is to use the built in repmat function:

repmat(A, n, 1)

Or you could use linear algebra:

ones(n,1)*A

Or you can use indexing:

A([ones(n,1), ones(n,1)*2])

Or as Luis Mendo points out, you might not even need to replicate it depending on your end goal as linear algebra or bsxfun might prove more efficient solutions.

Dan
  • 45,079
  • 17
  • 88
  • 157