MATLAB Creating m by m matrix in matlab

  • Thread starter Thread starter sugaku
  • Start date Start date
  • Tags Tags
    Matlab Matrix
Click For Summary
To create an m by m matrix from a 1 by m vector z in a single command, the repmat() function is recommended. The syntax repmat(z, size(z, 2), 1) effectively replicates the vector z across multiple rows, generating the desired matrix. This method is efficient and avoids the need for iterative loops, making it suitable for larger values of m.
sugaku
Messages
16
Reaction score
0
hi all,

i have a vector, z which size is 1 by m.
z = [1 2 3 4 5 6 7 8]

then i want to make a matrix with m by m
Z= [z; z; z; z; z; z; z; z]

if m become bigger i couldn't do it like mentioned above. how could i generate the matrix by a single command?

thank you in advance.
 
Physics news on Phys.org
A kind of brute fortish method would be
A=zeros(m)
for r=1:m
A(r,:)=z;
end

I don't know if there's a vectorized way to do it but there probably is
 
See the repmat() function. For your purpose its repmat(z, size(z)(2), 1)
 

Similar threads

Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
12K
  • · Replies 2 ·
Replies
2
Views
2K