MATLAB Answers

# How do you initialize an N*M matrix?

6,585 views (last 30 days)
Harry on 26 Jun 2013
Commented: Bogdan Goidescu on 30 Jan 2019
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 CommentsShowHide 2 older comments
Tulike on 12 Jul 2017
M=zeros(N,M)

Sign in to comment.

### Accepted Answer

Leah on 26 Jun 2013
Edited: MathWorks Support Team on 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”.
##### 1 CommentShowHide None
Bogdan Goidescu on 30 Jan 2019
ok,but,i have this problem:Write a program into a script file that creates a matrix m x n with elements that have the following values:
- The value of each element in the first line is the number of that column
- The value of each element in the first column is the line number
- Each of the other elements of the matrix have values ​​equal to the sum between the element above and the left element

Sign in to comment.

### More Answers (3)

Lokesh Ravindranathan on 26 Jun 2013
Edited: Lokesh Ravindranathan on 26 Jun 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 CommentsShowHide 1 older comment
Lokesh Ravindranathan on 26 Jun 2013
Oh. Thanks Isakson. I will update my answer. My MATLAB did not have symbolic Math toolbox.

Sign in to comment.

Nitin on 26 Jun 2013
you could initialize the matrix,
M = zeros(n,m);
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

Pau on 17 Oct 2018
This should make the trick
M = double.empty(N,M,0);
https://uk.mathworks.com/help/matlab/ref/empty.html
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!