two dimensional array in matlab

In MATLAB, a two-dimensional array is a matrix. To create a matrix, you can use the following syntax:

main.m
matrix_name = [row1; row2; row3; ... ]
39 chars
2 lines

For example, let's create a 2x3 matrix with the following elements:

main.m
1 2 3
4 5 6
12 chars
3 lines

To create this matrix, you can use the following code:

main.m
A = [1 2 3; 4 5 6];
20 chars
2 lines

Or, you can create each row separately and combine them using semicolons:

main.m
row1 = [1 2 3];
row2 = [4 5 6];
A = [row1; row2];
50 chars
4 lines

You can also create a matrix of zeros or ones using the following syntax:

main.m
zeros_matrix = zeros(m,n);
ones_matrix = ones(m,n);
52 chars
3 lines

Where m is the number of rows and n is the number of columns. If you want a matrix with a specific value, you can use the repmat function:

main.m
value = 4;
matrix = repmat(value, m, n);
41 chars
3 lines

This will create a mxn matrix with each element equal to value.

related categories

gistlibby LogSnag