Matlab Tutorials —— Vectorization

One of the most special features that difffers Matlab from other program languages is that Matlab is very well optimized for operation involved vectors and matrices. The problems that are usually done in a for loop in other languages, are likely to be done in a vectorization way in Matlab. Vectorization operations are more readable, less error prone and definitely have better performance. Therefore, vectorizing code is a general programming demand in Matlab.


The first simple example is here:

% loop
tic
i = 0;
for t = 0:0.01:10*pi
    i = i + 1;
    y(i) = sin(t);
end
toc

% vectorization
tic
t = 0:0.01:10*pi;
y = sin(t);
toc
Vectoriazed code is about 5 times faster


Another example:

% loop
tic
x = 1:10000;
ylength = (length(x) - mod(length(x),5))/5;
y(1:ylength) = 0;
for n= 5:5:length(x)
    y(n/5) = sum(x(1:n));
end 
toc

% vectorization
tic
x = 1:10000;
xsums = cumsum(x);  % cumulative summation, xsums = Σx
y = xsums(5:5:length(x)); 
toc

This is 35 times faster


Some other examples:

for n = 1:10000
   V(n) = 1/4 * pi * (D(n)^2) * H(n));
end
% vectorization
V = 1/4 * pi * (D.^2) .* H;
A = [97 89 84; 95 82 92; 64 80 99;76 77 67; 88 59 74; 78 66 87; 55 93 85];

mA = mean(A);  % mean by columns
B = zeros(size(A));
for n = 1:size(A,2)
    B(:,n) = A(:,n) - mA(n);
end
% vectorization
devA = A - mean(A);
% like meshgrid, expand matrices automatically
x = (-2:0.2:2)';  % 21-by-1
y = -1.5:0.2:1.5;  % 1-by-16
F = x.*exp(-x.^2-y.^2);  % 21-by-16
D = [-0.2 1.0 1.5 3.0 -1.0 4.2 3.14]; 
Dpositive = D(D > 0);
if all(D < 0)
    display('All values of diameter are negative.');
end
A = repmat(1:3,5,2);  % repeat 1:3 in 5 * 2 matrix form
B = repmat([1 2; 3 4],2,2);  % repeat [1 2; 3 4] in 2 * 2 matrix form
x = [2 1 2 2 3 1 3 2 1 3];
x = sort(x);
difference  = diff([x, max(x)+1]);
notRepeatingX = x(find(difference));  % return x without redundent elements
count = diff(find([1,difference]));  % count the times of appearance of each element

% count NaN and Inf
count_nans = sum(isnan(x(:)));
count_infs = sum(isinf(x(:)));


Useful appendix:

FunctionDescription
all

Determine if all array elements are nonzero or true

any

Determine if any array elements are nonzero

cumsum

Cumulative sum

diff

Differences and Approximate Derivatives

find

Find indices and values of nonzero elements

ind2sub

Subscripts from linear index

ipermute

Inverse permute dimensions of N-D array

logical

Convert numeric values to logicals

meshgrid

Rectangular grid in 2-D and 3-D space

ndgrid

Rectangular grid in N-D space

permute

Rearrange dimensions of N-D array

prod

Product of array elements

repmat

Repeat copies of array

reshape

Reshape array

shiftdim

Shift dimensions

sort

Sort array elements

squeeze

Remove singleton dimensions

sub2ind

Convert subscripts to linear indices

sum

Sum of array elements






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值