How do you initialize an N*M matrix?

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 Kommentare

What does
which matrix
return?
Harry
Harry am 27 Jun. 2013
'matrix' not found.
Tulike
Tulike am 12 Jul. 2017
M=zeros(N,M)

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Leah
Leah am 26 Jun. 2013
Bearbeitet: MathWorks Support Team am 27 Nov. 2018

7 Stimmen

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 Kommentare

Bogdan Goidescu
Bogdan Goidescu am 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
Abhishek Inamdar
Abhishek Inamdar am 6 Jul. 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
israt fatema am 25 Aug. 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (5)

Lokesh Ravindranathan
Lokesh Ravindranathan am 26 Jun. 2013
Bearbeitet: Lokesh Ravindranathan am 26 Jun. 2013

2 Stimmen

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 Kommentare

per isakson
per isakson am 26 Jun. 2013
Bearbeitet: per isakson am 26 Jun. 2013
matrix is a function in the symbolic toolbox.
Lokesh Ravindranathan
Lokesh Ravindranathan am 26 Jun. 2013
Oh. Thanks Isakson. I will update my answer. My MATLAB did not have symbolic Math toolbox.

Melden Sie sich an, um zu kommentieren.

Nitin
Nitin am 26 Jun. 2013

0 Stimmen

you could initialize the matrix,
M = zeros(n,m);
Pau
Pau am 17 Okt. 2018

0 Stimmen

This should make the trick
M = double.empty(N,M,0);
https://uk.mathworks.com/help/matlab/ref/empty.html
Arun
Arun am 7 Dez. 2022
Bearbeitet: Arun am 7 Dez. 2022

0 Stimmen

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.
HARSHAVARTHINI
HARSHAVARTHINI am 26 Nov. 2024

0 Stimmen

% Define the matrix A = [4 1 9; 0 1 3; 0 1 2];

% Initialize parameters n = size(A, 1); % Size of the matrix x = rand(n, 1); % Initial guess for the eigenvector tolerance = 1e-6; % Convergence criteria max_iter = 1000; % Maximum number of iterations lambda_old = 0; % Initial eigenvalue

for k = 1:max_iter % Multiply matrix A with vector x y = A * x;

    % Normalize the vector
    x = y / norm(y);
    % Compute the Rayleigh quotient (dominant eigenvalue)
    lambda = x' * A * x;
    % Check for convergence
    if abs(lambda - lambda_old) < tolerance
        fprintf('Converged in %d iterations.\n', k);
        break;
    end
    % Update old eigenvalue
    lambda_old = lambda;
end

% Display results fprintf('Dominant Eigenvalue: %.6f\n', lambda); disp('Corresponding Eigenvector:'); disp(x);

Kategorien

Mehr zu Matrices and Arrays finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 26 Jun. 2013

Beantwortet:

am 26 Nov. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by