# How do you initialize an N*M matrix?

1.698 visualizzazioni (ultimi 30 giorni)
Harry il 26 Giu 2013
Modificato: Arun il 7 Dic 2022
From the MATLAB help, it says to use:
M = matrix(N, M)
but when I apply this it says that the function 'matrix' is not recognized.
Undefined function 'matrix' for input arguments of type 'double'.
Error in experimental (line 1)
M = matrix(3,3)
##### 3 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Harry il 27 Giu 2013
Tulike il 12 Lug 2017
M=zeros(N,M)

Accedi per commentare.

### Risposta accettata

Leah il 26 Giu 2013
Modificato: MathWorks Support Team il 27 Nov 2018
To initialize an N-by-M matrix, use the “zeros” function. For example, create a 3-by-5 matrix of zeros:
A = zeros(3,5);
You can then later assign specific values to the elements of “A”.
##### 3 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Abhishek Inamdar il 6 Lug 2021
Use "X = ones(n)" and add the values based on the row and column. Use for loop to work on value addition
israt fatema il 25 Ago 2021
Can you please show me how to assign value to A after initialize the N x M matrix? For example i need to create a vector 5 x 5 and with values x = 20 35 49 64 23

Accedi per commentare.

### Più risposte (4)

Lokesh Ravindranathan il 26 Giu 2013
Modificato: Lokesh Ravindranathan il 26 Giu 2013
I am assuming you are trying to create an empty matrix of zeros of dimensions N*M. You can try the following instead
M = zeros(3,3)
This creates a matrix of zeros of size 3*3.
##### 2 CommentiMostra NessunoNascondi Nessuno
per isakson il 26 Giu 2013
Modificato: per isakson il 26 Giu 2013
matrix is a function in the symbolic toolbox.
Lokesh Ravindranathan il 26 Giu 2013
Oh. Thanks Isakson. I will update my answer. My MATLAB did not have symbolic Math toolbox.

Accedi per commentare.

Nitin il 26 Giu 2013
you could initialize the matrix,
M = zeros(n,m);
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Pau il 17 Ott 2018
This should make the trick
M = double.empty(N,M,0);
https://uk.mathworks.com/help/matlab/ref/empty.html
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Arun il 7 Dic 2022
Modificato: Arun il 7 Dic 2022
Here is the documentation for multi dementional arrays in matlab
Here is an example to create and initialize a 3X3 matric
A = [1 2 3; 4 5 6; 7 8 9]
A = 3×3
1 2 3
4 5 6
7 8 9
It is simular to matrix in c programming.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Matrices and Arrays 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