Professional Documents
Culture Documents
Description
length
ndims
numel
size
The following examples show some simple ways to use these functions. Both
use the 3-by-5 matrix A shown here:
A = 10*gallery('uniformdata',[5],0);
A(4:5, :) = []
A =
9.5013
7.6210
6.1543
4.0571
2.3114
4.5647
7.9194
9.3547
6.0684
0.1850
9.2181
9.1690
0.5789
3.5287
8.1317
1-22
6.15
6.07
Description
isa
iscell
iscellstr
ischar
isfloat
isinteger
1-23
1-25
Create a second array that is 3-by-2 and uses the same field names:
for k = 25:30
S2(k-24) = struct('x', 10*k, 'y', 10*k+1, 'z', 10*k+2);
end
S2= reshape(S2, 3, 2);
1-26
Try this again, but this time do it in such a way that enables MATLAB to
make adjustments to the size of the matrix. Store the new element in row 4, a
row that does not yet exist in this matrix. MATLAB expands matrix A by an
entire new row by padding columns 2 through 5 with zeros:
A(4,1) = 160
A =
10
20
60
70
110
120
160
0
30
80
130
0
40
90
140
0
50
100
150
0
1-27
10
60
110
108
103
104
20
70
120
101
105
109
30
80
130
106
107
102
40
90
140
0
0
0
50
100
150
0
0
0
You do not have to add new elements sequentially. Wherever you store
the new elements, MATLAB pads with zeros to make the resulting matrix
rectangular in shape:
A(4,8) = 300
A =
10
20
60
70
110
120
0
0
30
80
130
0
40
90
140
0
50
100
150
0
0
0
0
0
0
0
0
0
0
0
0
300
1-28
greeting(1,8:12) = 'World'
108
111
87
111
114
108
100
This causes some functions, like strcmp, to return what might be considered
an unexpected result:
strcmp(greeting, 'Hello
ans =
0
World')
3
10
6
15
13
8
12
1
1-29
3
10
6
15
13
8
12
1
If you delete a single element from a matrix, the result is not a matrix
anymore. So expressions like
A(1,2) = []
result in an error. However, you can use linear indexing to delete a single
element, or a sequence of elements. This reshapes the remaining elements
into a row vector:
A(2:2:10) = []
results in
A =
16
13
12
Reshaping a Matrix
The following functions change the shape of a matrix.
1-30
Function
Description
reshape
rot90
fliplr
flipud
flipdim
Function
Description
transpose
ctranspose
Examples
Here are a few examples to illustrate some of the ways you can reshape
matrices.
Reshaping a Matrix. Reshape 3-by-4 matrix A to have dimensions 2-by-6:
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
2
4
6
8
9
10
11
12
2
5
8
11
3
6
9
12
1-31
3.0000 + 7.0000i
6.0000 -4.0000i
4.0000 + 6.0000i
5.0000 -5.0000i
6.0000 + 4.0000i
12
9
6
3
4
5
6
1
2
3
Preallocating Memory
Repeatedly expanding the size of an array over time, (for example, adding
more elements to it each time through a programming loop), can adversely
affect the performance of your program. This is because
MATLAB has to spend time allocating more memory each time you increase
the size of the array.
This newly allocated memory is likely to be noncontiguous, thus slowing
down any operations that MATLAB needs to perform on the array.
1-32
The preferred method for sizing an array that is expected to grow over time
is to estimate the maximum possible size for the array, and preallocate this
amount of memory for it at the time the array is created. In this way, your
program performs one memory allocation that reserves one contiguous block.
The following command preallocates enough space for a 25,000 by 10,000
matrix, and initializes each element to zero:
A = zeros(25000, 10000);
% User-defined function
1-33
rp = rp + blocksize;
end
Note If you eventually need more room in a matrix than you had preallocated,
you can preallocate additional storage in the same manner, and concatenate
this additional storage onto the original array.
1-34
Description
circshift
sort
sortrows
issorted
You can sort matrices, multidimensional arrays, and cell arrays of strings
along any dimension and in ascending or descending order of the elements.
The sort functions also return an optional array of indices showing the order
in which elements were rearranged during the sorting operation.
1-35
A = [1:8;
A =
1
11
21
31
41
3
13
23
33
43
4
14
24
34
44
5
15
25
35
45
6
16
26
36
46
7
17
27
37
47
8
18
28
38
48
2
12
22
32
42
3
13
23
33
43
4
14
24
34
44
5
15
25
35
45
Now take A and shift it along both dimensions: three columns to the right
and two rows up:
A = [1:8; 11:18; 21:28; 31:38; 41:48];
B = circshift(A, [-2,
B =
26
27
28
36
37
38
46
47
48
6
7
8
16
17
18
3])
21
31
41
1
11
22
32
42
2
12
23
33
43
3
13
24
34
44
4
14
25
35
45
5
15
Since circshift circulates shifted rows and columns around to the other end
of a matrix, shifting by the exact size of A returns all rows and columns to
their original location:
B = circshift(A, size(A));
all(B(:) == A(:))
ans =
1
1-36
17
40
73
91
92
93
0
5
35
41
81
89
13
19
19
20
27
60
1
41
44
46
74
93
1
20
52
67
83
84
issorted(c(:, 1))
ans =
1
1-37
A=floor(gallery('uniformdata',[6 7],0)*100);
r = sort(A, 2, 'descend')
r =
95
92
84
45
89
74
73
52
82
60
44
20
93
67
60
48
93
89
83
81
91
79
76
41
41
23
19
44
61
19
13
20
17
40
46
1
1
1
5
35
27
0
issorted(fliplr(r(1, :)))
ans =
1
When you specify a second output, sort returns the indices of the original
matrix A positioned in the order they appear in the output matrix. In this next
example, the second row of index contains the sequence 4 3 2 5 1, which
means that the sorted elements in output matrix r were taken from A(2,4),
A(2,3), A(2,2), A(2,5), and A(2,1):
[r index] = sort(A, 2, 'descend');
index
index =
1
3
7
2
4
4
6
3
7
1
2
1
6
7
5
6
7
5
1
2
3
1
7
4
2
3
2
1
6
5
5
5
3
3
6
7
6
2
4
4
5
4
1-38
A=floor(gallery('uniformdata',[6 7],0)*100);
A =
95
45
92
41
13
1
84
23
1
73
89
20
74
52
60
82
17
5
19
44
20
48
44
40
35
60
93
67
89
61
93
81
27
46
83
76
79
91
0
19
41
1
To sort in ascending order based on the values in column 1, you can call
sortrows with just the one input argument:
sortrows(A)
r =
23
1
48
44
60
82
76
79
89
61
95
45
73
40
17
91
93
92
89
35
5
0
81
41
20
60
19
19
27
13
74
93
44
41
46
1
52
67
20
1
83
84
To base the sort on a column other than the first, call sortrows with a second
input argument that indicates the column number, column 4 in this case:
r = sortrows(A,
r =
76
79
60
82
48
44
95
45
89
61
23
1
4)
91
17
40
92
93
73
0
5
35
41
81
89
19
19
60
13
27
20
41
44
93
1
46
74
1
20
67
84
83
52
1-39
Description
blkdiag
diag
trace
tril
triu
1-40
0
0
0
24
0
0
0
0
0
0
28
0
0
0
0
0
0
32
You can also use the diag function to scan an existing matrix and return the
values found along one of the diagonals:
A = magic(5)
A =
17
24
23
5
4
6
10
12
11
18
diag(A, 2)
ans =
1
14
22
1
7
13
19
25
8
14
20
21
2
15
16
22
3
9
0
0
0
0
12
13
0
0
0
0
0
18
0
0
0
0
0
0
1-41