forked from anne-urai/Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectout.m
More file actions
26 lines (19 loc) · 782 Bytes
/
projectout.m
File metadata and controls
26 lines (19 loc) · 782 Bytes
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
function fullresidual = projectout(vector, whattoprojectout)
% projects one vector out of the other
% Anne Urai, 2015
% make sure everything is the same size
vector = vector(:); whattoprojectout = whattoprojectout(:);
% ignore nans
idx2use = (~isnan(vector));
idx2use(find(isnan(whattoprojectout))) = 0;
prj = whattoprojectout(idx2use)/norm(whattoprojectout(idx2use)); % take the norm of vector you want to project out
% project out the reference vector
residual = vector(idx2use) - (vector(idx2use)'*prj)*prj; % subtract dot product, and that's it
% in case there were NaNs, return those in the output
fullresidual = nan(size(vector));
fullresidual(idx2use) = residual;
if all(isnan(fullresidual))
fullresidual = vector;
fullresidual(idx2use) = residual;
end
end