create a matrix in matlab

To create a matrix in MATLAB, you can use the following syntax:

main.m
A = [1 2 3; 4 5 6; 7 8 9]
26 chars
2 lines

This creates a 3-by-3 matrix called A with values 1 through 9 arranged in row-major order.

Alternatively, you can also create a matrix using the zeros or ones functions:

main.m
B = zeros(4, 3)
16 chars
2 lines

This creates a 4-by-3 matrix called B filled with 0s.

main.m
C = ones(2, 5)
15 chars
2 lines

This creates a 2-by-5 matrix called C filled with 1s.

You can also create a matrix using a combination of both scalar values and expressions. For example:

main.m
D = [1 2 3; 4*5 5+6 6-7; 8 9/3 10]
35 chars
2 lines

This creates a 3-by-3 matrix called D with values 1 through 10 arranged in row-major order, and where the second row is filled with expressions.

gistlibby LogSnag