-
Notifications
You must be signed in to change notification settings - Fork 2
/
vmag2.m
52 lines (39 loc) · 1.06 KB
/
vmag2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function vm2 = vmag2(v)
% VMAG2 Squares of magnitudes (2-norm) of each vector (column) of input
%
% Calculate square of the magnitude (2-norm) of each vector (column) of the
% input.
%
% vm2 = VMAG2(v)
%
% Inputs:
%
% v Set of vector(s) (3-by-n)
%
% Outputs:
%
% vm2 Squares of magnitudes (2-norm) (1-by-n)
%
% See also: vmag, normalize
% Copyright 2016 An Uncommon Lab
%#codegen
% 3D vectors are common, and this generates better code than for the
% general case.
if size(v, 1) == 3
vm2 = v(1,:).*v(1,:) + v(2,:).*v(2,:) + v(3,:).*v(3,:);
% Otherwise, for some other dimensionality...
else
% If running in regular MATLAB, vectorize.
if isempty(coder.target)
vm2 = sum(v.*v, 1);
% Otherwise, when running in some type of embedded code, use efficient
% (non-vectorized) code.
else
n = size(v, 2);
vm2 = zeros(1, n, class(v));
for k = 1:n
vm2(k) = v(:,k).' * v(:,k);
end
end
end
end % vmag2