set size of matrix in matlab

To set the size of a matrix in MATLAB, you can use the zeros() or ones() function to create a matrix of the desired size. To create a matrix of size m by n, you can use:

main.m
A = zeros(m, n); % creates a matrix of zeros with size m x n
B = ones(m, n); % creates a matrix of ones with size m x n
120 chars
3 lines

If you want to change the size of an existing matrix A, you can use the size() function. For example, to change the size of A to m by n, you can use:

main.m
A = A(1:m, 1:n); % extracts the submatrix of size m x n from A
63 chars
2 lines

Alternatively, if you want to change the number of rows or columns of A without changing the total number of elements, you can use:

main.m
A = reshape(A, m, n); % reshapes A to have m rows and n columns
64 chars
2 lines

You can also use indexing to set the size of a matrix. For example, to create a matrix A with m rows and n columns, you can use:

main.m
A = [];
for i = 1:m
    A = [A; zeros(1, n)];
end
50 chars
5 lines

This creates an empty matrix A, and then appends m rows of zeros, each with n columns.

gistlibby LogSnag