Array Functions

 

size, length:

M = randi([0,10], 3, 4)
>>
  2    1    5   6
  6   10    2   9
  0    1   10   2

N = randi([0,10], 3, 2)
>>
  7    6
  5   10
  9    9
size(M)
% size of a matrix M as a 1x2 vector
% representing #rows and #columns of the matrix
>>  3   4

size(M,1)
>>  3

size(M,2)
>>  4
length(M)
% size of the longest dimension of matrix M,
% equivalent to max(size(M))
>>  4
size(N)
>>  3   2

length(N)
>>  3

find, any, all:

find([4 0 5 9])
% Find indices of nonzero elements
>>  1    3    4
X = [1  0  0; 0  0  0; 0  1  1]
>>
  1  0  0
  0  0  0
  0  1  1

find(X)
>>
  1
  6
  9
% X(1), X(6), and X(9) are nonzero elements
[r,c] = find(X)
>>
 r =
    1
    3
    3
 c =
    1
    2
    3
% (1,1), (3,2), and (3,3) are nonzero elements

any([0; 0; 0; 3])
% Test vector for any nonzero elements
>>  1   % (logical)

any([0; 0; 0; 0])
>>  0

any([0 0 0 3])
>>  1

any([0 0 0 0])
>>  0
any([0 0; 0 0; 0 0; 0 3])
% Test each column for any nonzero elements
>>  0   1   % (1x2 logical array)

any([0 0; 0 0; 0 0; 0 0])
>>  0   0

all([1; 0; 2; 3])
% Test vector for all nonzero elements
>>  0   % (logical)

all([1; 4; 2; 3])
>>  1

all([1 0 2 3])
>>  0

all([1 4 2 3])
>>  1
all([0 1; 0 0; 0 2; 0 3])
% Test each column for all nonzero elements
>>  0   0   % (1x2 logical array)

all([0 1; 0 4; 5 2; 0 3])
>>  0   1

flipud, fliplr, flip:

A = magic(3);
>>
  8   1   6
  3   5   7
  4   9   2

flipud(A)   % or flip(A, 1)
% Flip array up to down (along 1st dimension)
% i.e. reverses the elements in each column
>>
  4   9   2
  3   5   7
  8   1   6

fliplr(A)   % or flip(A, 2)
% Flip array left to right (along 2nd dimension)
% i.e. reverses the elements in each row
>>
  6   1   8
  7   5   3
  2   9   4

flip(A):

  • If A is vector, then flip(A) reverses the order of the elements along the vector length.
  • If A is a matrix, then flip(A) reverses the order of the elements in each column.
A = magic(3);
>>
  8   1   6
  3   5   7
  4   9   2

flip(A)
% Reverses the elements in each column (as A is a matrix)
% Equivalent to flip(A, 1) only if A is a multi-dimensional matrix
>>
  4   9   2
  3   5   7
  8   1   6
B = 1:4;
>>  1   2   3   4

flip(B)
% Reverses the elements along the vector length
>>  4   3   2   1

fliplr(B)
>>  4   3   2   1

flipud(B)
>>  1   2   3   4
C = B';
>>
  1
  2
  3
  4

flip(C)
% Reverses the elements along the vector length
>>
  4
  3
  2
  1

fliplr(C)
>>
  1
  2
  3
  4

flipud(C)
>>
  4
  3
  2
  1

inv, pinv:

A = magic(3);
>>  8   1   6
    3   5   7
    4   9   2

inv(A)
>>
   0.147222  -0.144444   0.063889
  -0.061111   0.022222   0.105556
  -0.019444   0.188889  -0.102778

A*inv(A)
>>
  1.00000  -0.00000  -0.00000
  0.00000   1.00000   0.00000
  0.00000   0.00000   1.00000
pinv(A)
>>
  0.147222  -0.144444   0.063889
 -0.061111   0.022222   0.105556
 -0.019444   0.188889  -0.102778

A*pinv(A)
>>
  1.0000e+00  -1.2157e-14   6.3560e-15
  5.5511e-17   1.0000e+00  -1.5266e-16
 -5.9813e-15   1.2268e-14   1.0000e+00

round(A*pinv(A))
>>
  1  -0   0
  0   1  -0
 -0   0   1