-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmc_flatten_upper_triangle.m
More file actions
30 lines (21 loc) · 1008 Bytes
/
mc_flatten_upper_triangle.m
File metadata and controls
30 lines (21 loc) · 1008 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
27
28
29
30
function [ flatmat ] = mc_flatten_upper_triangle( squaremat , censor)
% Flatten Upper Trianngle - A function to extract, as a vector, the upper
% triangular elements of a square matrix. At present, if any elements in
% the upper triangle are inf, this will lead to an error.
% NOTE - It will strip logicals down to doubles and return as such
squaremat = +squaremat; % Coerce away from logical
% Check if any elements are inf.
if any(isnan(squaremat(:)))
% raise error message and abort. Add this functionality once mc_error
% is written.
end
% Protect any zero-valued elements by changing to inf.
squaremat_protected = squaremat;
squaremat_protected(squaremat_protected==0) = Inf;
% Zero out all elements except upper triangle, then reshape to flat vector
flatmat_full = reshape(triu(squaremat_protected,1),1,size(squaremat_protected(:),1));
% Drop all zero elements
flatmat = flatmat_full(flatmat_full~=0);
% Restore any zeros in original square matrix
flatmat(isinf(flatmat)) = 0;
end