# How to constructing a matrix of repeated rows v?

15 visualizzazioni (ultimi 30 giorni)
Cantor Set il 7 Mar 2020
Modificato: Matt J il 7 Mar 2020
If I have an arbitrary matrix A and a row vector v of 1 row and the same number of columns as A.
Suppose I want to construct a matrix Q of the same size as A where every row in Q is the vector v.
Illustrative example:
A=[ 1 1 1; 2 2 2; 3 3 3; 4 4 4]; v=[5 5 5];
%I want to create Q s.t
Q=[5 5 5; 5 5 5 5; 5 5 5 5; 5 5 5 5];
Thanks
##### 0 CommentiMostra -1 commenti meno recentiNascondi -1 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Matt J il 7 Mar 2020
Modificato: Matt J il 7 Mar 2020
Either of the following would work
>> Q=repelem(v,size(A,1),1)
>> Q=repmat(v,size(A,1),1)
but note that it's not normally something you need or want to do. Matlab has deliberately provided implicit expansion,
or for older Matlab versions bsxfun,
to avoid such copying in many common situations.
##### 0 CommentiMostra -1 commenti meno recentiNascondi -1 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Matrix Indexing in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by