MATLAB: filling the matrix with each column is the same

I am trying to create a matrix that is 3 xn, with each of the columns being the same. What is the easiest way to achieve it? Concatenation?

+5
source share
3 answers

After

n=7
x=[1;2;3]

it either

repmat(x,[1 n])

or

x(:,ones(1,n))
+8
source

(Octave can be thought of as an open source version / a free version of MATLAB)

octave-3.0.3:2> rowvec = [1:10]
rowvec =

    1    2    3    4    5    6    7    8    9   10

octave-3.0.3:3> [rowvec; rowvec; rowvec]
ans =

    1    2    3    4    5    6    7    8    9   10
    1    2    3    4    5    6    7    8    9   10
    1    2    3    4    5    6    7    8    9   10

Use repmatif the number of rows is large.

octave-3.0.3:7> repmat(rowvec, 10, 1)
ans =

    1    2    3    4    5    6    7    8    9   10
    1    2    3    4    5    6    7    8    9   10
    1    2    3    4    5    6    7    8    9   10
    1    2    3    4    5    6    7    8    9   10
    1    2    3    4    5    6    7    8    9   10
    1    2    3    4    5    6    7    8    9   10
    1    2    3    4    5    6    7    8    9   10
    1    2    3    4    5    6    7    8    9   10
    1    2    3    4    5    6    7    8    9   10
    1    2    3    4    5    6    7    8    9   10
+3
source

1 × 3

, x * [1 1 1]

Edit:

:

    octave-3.0.3.exe:1> x = [1;2;3;4]
x =

   1
   2
   3
   4


octave-3.0.3.exe:5> x * [1 1 1]
ans =

   1   1   1
   2   2   2
   3   3   3
   4   4   4
+2

All Articles