| MATLAB® | ![]() |
| On this page… |
|---|
These functions return information about the shape and size of a matrix.
Function | Description |
|---|---|
Return the length of the longest dimension. (The length of a matrix or array with any zero dimension is zero.) | |
Return the number of dimensions. | |
Return the number of elements. | |
Return the length of each dimension. |
The following examples show some simple ways to use these functions. Both use the 3-by-5 matrix A shown here:
rand('state', 0); % Initialize random number generator
A = rand(5) * 10;
A(4:5, :) = []
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
Using the numel function, find the average of all values in matrix A:
sum(A(:))/numel(A)
ans =
5.8909
Using ndims and size, go through the matrix and find those values that are between 5 and 7, inclusive:
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
The code returns the following:
A(1, 3) = 6.15 A(3, 1) = 6.07
These functions test elements of a matrix for a specific data type.
Function | Description |
|---|---|
Detect if input is of a given data type. | |
Determine if input is a cell array. | |
Determine if input is a cell array of strings. | |
Determine if input is a character array. | |
Determine if input is a floating-point array. | |
Determine if input is an integer array. | |
Determine if input is a logical array. | |
Determine if input is a numeric array. | |
Determine if input is an array of real numbers. | |
Determine if input is a MATLAB® structure array. |
Pick out the real numeric elements from this vector:
A = [5+7i 8/7 4.23 39j pi 9-2i];
for m = 1:numel(A)
if isnumeric(A(m)) && isreal(A(m))
disp(A(m))
end
end
The values returned are
1.1429
4.2300
3.1416
These functions test elements of a matrix for a specific data structure.
Function | Description |
|---|---|
Determine if input has any dimension with size zero. | |
Determine if input is a 1-by-1 matrix. | |
Determine if input is a sparse matrix. | |
Determine if input is a 1-by-n or n-by-1 matrix. |
![]() | Matrix Indexing | Resizing and Reshaping Matrices | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |