Matrix Creation
Directly declare a matrix using semicolons to separate different rows.
>> a = [1 2 3; 4 5 6; 7 8 9]
a =
1 2 3
4 5 6
7 8 9
You can also quickly create a matrix with functions such as ones, zeros and rand.
For instance, ones(3,4) is to create a 3-by-1 column matrix of ones.
>> b = ones(3,4)
b =
1 1 1 1
1 1 1 1
1 1 1 1
Matrix Operations
MATLAB allows you to process all of the values in a matrix using a single arithmetic operator or function.
>> a+10
ans =
11 12 13
14 15 16
17 18 19
>> sin(a)
ans =
0.8415 0.9093 0.1411
-0.7568 -0.9589 -0.2794
0.6570 0.9894 0.4121
Operators:
A = [1 2 3 4; 5 6 7 8]
B = [1 1 2 2; 2 2 1 1]
C = A + B
D = A - B
E = A * B'
F = A .* B
G = A / B % B * G = A
H = A ./ B
Clearly * is a matrix operation, and .* is element-wise.
Concatenation:
>> A = [a,a]
A =
1 2 3 1 2 3
4 5 6 4 5 6
7 8 9 7 8 9
Indexing
Indexing is to get you the selected element.
>> a(2,3)
ans =
6
>> a(2,:)
ans =
4 5 6
>> a(2,1:2:3)
ans =
4 6
MATLAB ® Primer © COPYRIGHT 1984–2017 by The MathWorks, Inc