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
313defa7
Commit
313defa7
authored
3 months ago
by
Achilles Kappis
Browse files
Options
Downloads
Patches
Plain Diff
Add diagCorr function to measure the diagonality of matrices
parent
aaa115ee
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/diagCorr.m
+81
-0
81 additions, 0 deletions
Utilities/Generic/MATLAB/Functions/diagCorr.m
with
81 additions
and
0 deletions
Utilities/Generic/MATLAB/Functions/diagCorr.m
0 → 100644
+
81
−
0
View file @
313defa7
%% Calculate a "diagonality" measure/metric
% --------------------------------------------------
% Author: Achilles Kappis
% e-mail: axilleaz@protonmail.com
%
% Date: 17/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 the
% correlation coefficient of a matrix to a diagonal matrix.
% --------------------------------------------------
% Input
%
% mat [numeric]: The matrices to be "checked" for "diagonality". This can
% 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.
%
% --------------------------------------------------
% Output
%
% diagonality [numeric]: This is the diagonality correlation coefficient
% for each of the arrays provided. This will be
% either a scalar, if M is 2D, or a vector if M is
% 3D, with each element corresponing to the
% diagonality metric for each matrix.
%
% --------------------------------------------------
% Notes
%
% - The metric is calculated as provided in the Mathematics StackExchange
% at https://math.stackexchange.com/a/1393907.
%
% --------------------------------------------------
function
[
diagonality
]
=
diagCorr
(
mat
)
% ====================================================
% Check for number of arguments
% ====================================================
narginchk
(
1
,
1
);
nargoutchk
(
0
,
1
);
% ====================================================
% Validate input arguments
% ====================================================
% Validate mandatory arguments
validateattributes
(
mat
,
"numeric"
,
{
'3d'
,
'square'
,
'nonempty'
},
mfilename
,
"Matrices to be checked for diagonality"
,
1
);
% ====================================================
% Calculate auxiliary vectors
% ====================================================
elemSum
=
ones
(
1
,
size
(
mat
,
1
));
r
=
1
:
length
(
elemSum
);
r2
=
r
.^
2
;
% ====================================================
% Calculate correlation coefficient to diagonal matrix
% ====================================================
% 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
)
=
(
n
*
sumRowCol
-
sumRow
*
sumCol
)/(
sqrt
(
n
*
sumRow2
-
(
sumRow
^
2
))
*
sqrt
(
n
*
sumCol2
-
(
sumCol
^
2
)));
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