Vectorization and Logical Arrays
Memory Allocation
When you add an element to a MATLAB matrix beyond the current bounds of the matrix, it automatically expands. However, this process of allocating memory takes time. Always know the size of the data structure ahead of time.
function squares()
for n=1:100000
a(n) = n^2
end
end
90 seconds
function squares()
a = zeros(1,100000);
for n=1:100000
a(n) = n^2
end
end
0.004 seconds
Looping vs Vectorizing
Math operators are not always more efficient than using a looping construct. However, some tricks are available if you cast your code as matrix operations that make your code more elegant, easier to read, and possibly faster.
Example of equivalent functions:
function n=flipcoins()
n=0;
for m = 1:1000000
q = rand(1);
if (q>0.5)
n = n+1;
end
end
end
0.15s
function n=flipcoins()
q=rand(1000000,1);
n = sum(q>0.5);
end
0.09s
Logical Indexing
Consider a=[2 5 7; 8 9 1]
subscripted indexing: a(1,3), a([2.1]), a(:,3)
linear indexing a(4)
In logical indexing, the index is a logical matrix of the same size as the referenced matrix. The result is that the expression refers only to those elements in the referenced matrix that corresponds to the 1 (TRUE) vlaues in the indexing matrix.
When alone or on the right hand side, use logical indexing to provide a single column vector that contains all the values for which the indexing matrix is 1.
a=[5 3 2; 6 1 0];
q = logical([1 0 1; 1 0 0]);
a(q)
Results in a column vector of [5;6;2]
a=[5 3 2; 6 1 0];
r = a < 3;
a(r)
Results in a column vector of `[1;2;0]
You can also use it to change those values that satisfies r:
a(r)=-1
Results in [5 3 -1; 6 -1 -1]
function calcmatrix() %.09s
X=zeros(2001);
a=1;
for m = -1:0.01:1
b=1;
for n = -1:0.01:1;
X(a,b) = m^2+n^2;
if X(a,b) > 1
X(a,b)=1;
end
end
a=a+1;
end
end
It can be simplified as:
function calcmatrix() %0.007s
[n,m]=meshgrid(-1:0.01:1,-1:0.01:1);
X = m.^2+n.^2; X(X>1)=1;
end
Helper functions
all returns true if all elements are true
any returns true if any elements are true
helpful for vectorizing code
sum returns the sum along a dimension
cumsum returns the cumulative sum along a specified dimension. usage: cumsum([MATRIX],DIMENSION) (1 is row, 2 is column)
prod returns the product along a dimension
cumprodreturns the cumulative product along a specified dimension
prod return differences between adjacent elements. (approx. derivative)
sub2ind returns the index given the subscripts
ind2sub returns the subscripts given the index
permute permutes the dimensions of a matrix
ipermute inverse permutes the dimensions
shiftdim shift array dimensions
circshift shifts all elements in a specified direction
reshape reshape the array
find returns the indices of nonzero elements (linear indexing)
sort sort elements along a dimension
unique returns the unique elements in an array
meshgrid returns a mesh in x y order
ndgrid returns a mesh in row column order
repmat replicate and tile an array
squeeze eliminate singleton dimensions
Linked from Computational Modeling