Creating m by m matrix in matlab

In summary, the conversation is about creating a matrix with multiple rows of the same vector, z. The current method involves using a for loop, but there may be a vectorized approach using the repmat() function.
  • #1
sugaku
17
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
  • #2
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
 
  • #3
See the repmat() function. For your purpose its repmat(z, size(z)(2), 1)
 

1. How do I create an m by m matrix in MATLAB?

To create an m by m matrix in MATLAB, you can use the "zeros" or "ones" function and specify the dimensions as m and m. For example, to create a 5 by 5 matrix filled with zeros, you would use the command "zeros(5,5)".

2. Can I create a matrix with specific values in MATLAB?

Yes, you can create a matrix with specific values by using the "eye" function. This function creates an identity matrix with the specified dimensions. For example, to create a 3 by 3 matrix with the values 1, 2, and 3 on the diagonal, you would use the command "eye(3)*[1;2;3]".

3. How can I add or remove rows and columns from a matrix in MATLAB?

To add or remove rows and columns from a matrix in MATLAB, you can use the "cat" function. This function allows you to concatenate matrices along a specified dimension. For example, to add a row of zeros to the bottom of a matrix, you would use the command "cat(1, matrix, zeros(1,size(matrix,2)))".

4. Is it possible to create a matrix with random values in MATLAB?

Yes, you can create a matrix with random values in MATLAB by using the "rand" or "randn" function. The "rand" function generates uniformly distributed random numbers, while the "randn" function generates normally distributed random numbers. You can specify the dimensions of the matrix as well as the range of values to be generated.

5. How do I access and modify specific elements in a matrix in MATLAB?

To access and modify specific elements in a matrix in MATLAB, you can use indexing. Indexing allows you to specify the row and column of the element you want to access or modify. For example, to access the element in the second row and third column of a matrix, you would use the syntax "matrix(2,3)". You can also use indexing to assign new values to specific elements in a matrix.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
544
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Back
Top