-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnormr.m
70 lines (57 loc) · 1.28 KB
/
normr.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function nrlist = normr(rlist)
% NORMR normalizes vectors row-by-row. Outputs zero vector if zero vector input (shadowed by Computer Vision Toolbox)
%--------------------------------------------------------------------------
% Author: Sterling Baird
%
% Date: 2020-07-03
%
% Inputs:
%
% rlist === rows of vectors to be normalized
%
% Outputs:
%
% nrlist === normalized rows of vectors
%
% Dependencies:
%
% Note: normr.m shadows a built-in function that's part of the Computer
% Vision Toolbox
%
%--------------------------------------------------------------------------
%determine size
[n,d] = size(rlist);
% shortcut if one row
if n == 1
nm = norm(rlist);
if nm ~= 0
nrlist = rlist./norm(rlist);
else
nrlist = zeros(n,d);
end
else
%initialize
nrlist = zeros(size(rlist));
%compute norms
nmlist = vecnorm(rlist,2,2);
%get indices of non-zero elements
ids = find(nmlist);
%normalize only rows where norm is non-zero
nrlist(ids,:) = rlist(ids,:)./nmlist(ids);
%note: when nm(id) == 0, nrlist(id) == zeros(1,d)
end
end %normr
%-----------------------------CODE GRAVEYARD-------------------------------
%{
for i = 1:npts
r = rlist(i,:);
nm = nmlist(i);
if nm ~= 0
nr = r./nm;
else
nr = zeros(size(r));
end
nrlist(i,:) = nr;
end
% ids = nmlist ~= 0;
%}