Creating and catenating Matrices
A = 100;
Name Size Bytes Class
A 1x1 8 double array
Constructing a Simple Matrix
row1 = [E1, E2, ..., Em] row2 = [E1 E2 ... Em] …..
A = [12 62 93 -8 22];
A = [row1; row2; ...; rown]
A = [12 62 93 -8 22; 16 2 87 43 91; -4 17 -72 95 6]
A =
12 62 93 -8 22
16 2 87 43 91
-4 17 -72 95 6
Accessing Single Elements
A(row, column)
A = magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
A(4, 2)
ans = 14
Example Using numel
sum(A(:))/numel(A)
ans =
5.8909
Example Using ndims, numel, and size
if ndims(A) ~= 2
return
end
[rows cols] = size(A);
for m = 1:rows
for n = 1:cols
x = A(m, n);
if x >= 5 && x <= 7
disp(sprintf('A(%d, %d) = %5.2f', m, n, A(m,n)))
end
end
end
A(1, 3) = 6.15
A(3, 1) = 6.07
Reshaping a Matrix
A = [1 4 7 10; 2 5 8 11; 3 6 9 12]
A =
1 4 7 10
2 5 8 11
3 6 9 12
B = reshape(A, 2, 6)
B =
1 3 5 7 9 11
2 4 6 8 10 12
Shifting the Location of Matrix Elements
A = [1:8; 11:18; 21:28; 31:38; 41:48]
A =
1 2 3 4 5 6 7 8
11 12 13 14 15 16 17 18
21 22 23 24 25 26 27 28
31 32 33 34 35 36 37 38
41 42 43 44 45 46 47 48
B = circshift(A, [0, 3])
B =
6 7 8 1 2 3 4 5
16 17 18 11 12 13 14 15
26 27 28 21 22 23 24 25
36 37 38 31 32 33 34 35
46 47 48 41 42 43 44 45
Sorting the Data in Each Column
sort(matrix, dimension)
A = rand(3,5) * 10
A =
9.5013 7.6210 6.1543 4.0571 0.5789
2.3114 4.5647 7.9194 9.3547 3.5287
6.0684 0.1850 9.2181 9.1690 8.1317
c = sort(A, 1)
c =
2.3114 0.1850 6.1543 4.0571 0.5789
6.0684 4.5647 7.9194 9.1690 3.5287
9.5013 7.6210 9.2181 9.3547 8.1317
issorted(c(:, 1))
ans =
1
Constructing a Matrix from a Diagonal Vector
A = diag([12:4:32])
A =
12 0 0 0 0 0
0 16 0 0 0 0
0 0 20 0 0 0
0 0 0 24 0 0
0 0 0 0 28 0
0 0 0 0 0 32
Empty Matrices, Scalars, and Vectors
Column Vector Row Vector
53.2 53.2 87.39 4-12i 43.9
87.39
4-12i
43.9
0 comments:
Post a Comment