Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Virtual Sensing
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SPAH
IN-NOVA
Virtual Sensing
Commits
dc0cef11
Commit
dc0cef11
authored
3 months ago
by
Achilles Kappis
Browse files
Options
Downloads
Patches
Plain Diff
Change the diagCorr function to diagMetric because the former did not provide good results
parent
aa3aac23
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Utilities/Generic/MATLAB/Functions/diagMetric.m
+107
-0
107 additions, 0 deletions
Utilities/Generic/MATLAB/Functions/diagMetric.m
with
107 additions
and
0 deletions
Utilities/Generic/MATLAB/Functions/diag
Corr
.m
→
Utilities/Generic/MATLAB/Functions/diag
Metric
.m
+
107
−
0
View file @
dc0cef11
...
...
@@ -3,13 +3,13 @@
% Author: Achilles Kappis
% e-mail: axilleaz@protonmail.com
%
% Date: 1
7
/02/2025 (DD/MM/YYYY)
% Date: 1
8
/02/2025 (DD/MM/YYYY)
%
% Copyright: MIT
% --------------------------------------------------
% Functionality: Calculate a "diagonality" measure
. This provides a notion
%
"how diagonal" a matrix is. In practice this is
th
e
%
correlation coefficient of a matrix to a
diagonal
matrix
.
% Functionality: Calculate a "diagonality" measure
based on the norm of the
%
difference of between a matrix and the same matrix wi
th
%
its principal
diagonal
removed
.
% --------------------------------------------------
% Input
%
...
...
@@ -17,7 +17,25 @@
% be either a 2D array (matrix) or a 3D array (stacked
% matrices), in which case the first two dimensions will be
% treated as matrices of which the "diagonality" will be
% checked. The matrices must be square.
% checked. The matrices must be square (the first two
% dimensions must be equal).
%
% matNorm [numeric/char/string] (Optional): The norm to be used in the
% calculation of the diagonality
% metric. This can take one of
% the values 1, 2, and Inf, that
% define the L1, L2 or Linf
% norms, or be one of the strings
% "Fro", or "Frobenius" (not
% case-sensitive) to use the
% Frobenius norm.
% [Default: "Fro"].
%
% normalise [logical] (Optional): Whether to normalise the result with the
% norm of the matrix. This will provide a
% results between 0, when all the elements
% along the diagonal are zero and 1 when
% the matrix is diagonal.
%
% --------------------------------------------------
% Output
...
...
@@ -31,15 +49,17 @@
% --------------------------------------------------
% Notes
%
% - The metric is calculated as provided in the Mathematics StackExchange
% at https://math.stackexchange.com/a/1393907.
% - The metric is calculated as norm(A - diag(diag(A))), where the
% norm used is the one defined by the argument "matNorm". If "normalise"
% is true, the metric is calculated as
% 1 - norm(A - diag(diag(A))/norm(A).
%
% --------------------------------------------------
function
[
diagonality
]
=
diag
Corr
(
mat
)
function
diagonality
=
diag
Metric
(
mat
,
matNorm
,
normalise
)
% ====================================================
% Check for number of arguments
% ====================================================
narginchk
(
1
,
1
);
narginchk
(
1
,
3
);
nargoutchk
(
0
,
1
);
% ====================================================
...
...
@@ -48,34 +68,40 @@ function [diagonality] = diagCorr(mat)
% Validate mandatory arguments
validateattributes
(
mat
,
"numeric"
,
{
'3d'
,
'square'
,
'nonempty'
},
mfilename
,
"Matrices to be checked for diagonality"
,
1
);
% Validate optional arguments
if
nargin
>
1
&&
~
isempty
(
matNorm
)
% Make sure we have either a string-like object or a numeric scalar
validateattributes
(
matNorm
,
{
'char'
,
'string'
,
'numeric'
},
{
'scalar'
,
'nonempty'
,
'real'
},
mfilename
,
"The matrix norm to be used"
,
2
);
% ====================================================
% Calculate auxiliary vectors
% ====================================================
elemSum
=
ones
(
1
,
size
(
mat
,
1
));
r
=
1
:
length
(
elemSum
);
r2
=
r
.^
2
;
if
nnz
(
matNorm
==
[
1
,
2
,
Inf
])
==
0
&&
nnz
(
strcmpi
(
matNorm
,
[
"Fro"
,
"Frobenius"
]))
==
0
error
(
"The 'matNorm' argument can be either 1, 2, Inf, 'Fro', or 'Frobenius'."
);
end
if
~
isnumeric
(
matNorm
)
matNorm
=
lower
(
matNorm
);
end
else
matNorm
=
"fro"
;
end
if
nargin
>
2
&&
~
isempty
(
normalise
)
validateattributes
(
normalise
,
{
'logical'
,
'numeric'
},
{
'scalar'
,
'nonempty'
,
'real'
,
'finite'
,
'nonnan'
},
mfilename
,
"Normalisation flag"
,
3
);
normalise
=
logical
(
normalise
);
else
normalise
=
true
;
end
% ====================================================
% Calculate
correlation coefficient to diagonal
m
a
tri
x
% Calculate
the
m
e
tri
c
% ====================================================
% Go through the matrices
for
idx
=
size
(
mat
,
3
):
-
1
:
1
% Get the matrix at the current index
tmpMat
=
mat
(:,
:,
idx
);
% Calculate auxiliary values
n
=
sum
(
tmpMat
,
"all"
);
sumRow
=
r
*
tmpMat
*
elemSum
'
;
sumCol
=
elemSum
*
tmpMat
*
r
'
;
sumRow2
=
r2
*
tmpMat
*
elemSum
'
;
sumCol2
=
elemSum
*
tmpMat
*
r2
'
;
sumRowCol
=
r
*
tmpMat
*
r
'
;
diagonality
(
idx
)
=
norm
(
tmpMat
-
diag
(
diag
(
tmpMat
)),
matNorm
);
diagonality
(
idx
)
=
(
n
*
sumRowCol
-
sumRow
*
sumCol
)/(
sqrt
(
n
*
sumRow2
-
(
sumRow
^
2
))
*
sqrt
(
n
*
sumCol2
-
(
sumCol
^
2
)));
if
normalise
diagonality
(
idx
)
=
1
-
diagonality
(
idx
)/
norm
(
tmpMat
,
matNorm
);
end
end
% Get rid of possible imaginary residuals
diagonality
=
real
(
diagonality
);
end
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment