From 42094bea274e7ff7a7fd3e0946fd620a8522dc46 Mon Sep 17 00:00:00 2001 From: eminSerin Date: Sun, 21 Apr 2024 19:00:27 +0200 Subject: [PATCH] - Add function to check if a given matrix symmetric with tolerance. - Fix minor bug in NBSPredict_predict. --- src/NBSPredict_predict.m | 3 ++- src/io/MatrixVectorizer.m | 2 +- src/io/is_symmetric.m | 41 ++++++++++++++++++++++++++++++ src/io/shrinkMat.m | 2 +- test/networks/networks/gen_BAnet.m | 2 +- 5 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 src/io/is_symmetric.m diff --git a/src/NBSPredict_predict.m b/src/NBSPredict_predict.m index 119af43..23ce32c 100644 --- a/src/NBSPredict_predict.m +++ b/src/NBSPredict_predict.m @@ -114,7 +114,8 @@ end % Shrink connectivity matrices into edge matrices. -X = connData(model.preprocess.edgeIdx)'; +X = shrinkMat(connData); + % Predicts label using given data. if ~isempty(model.preprocess.scaler) diff --git a/src/io/MatrixVectorizer.m b/src/io/MatrixVectorizer.m index a76a838..3d0ce5f 100644 --- a/src/io/MatrixVectorizer.m +++ b/src/io/MatrixVectorizer.m @@ -98,7 +98,7 @@ else for i = 1:subjects cMat = data(:,:,i); % Select current matrix. - if ~issymmetric(cMat) + if ~is_symmetric(cMat) warning('Given matrix is not symmetric! Data index: %d', i) end edgeMat(i,:)=cMat(obj.edgeIdx); % extract edge values. diff --git a/src/io/is_symmetric.m b/src/io/is_symmetric.m new file mode 100644 index 0000000..d35eb7f --- /dev/null +++ b/src/io/is_symmetric.m @@ -0,0 +1,41 @@ +function isSymmetric = is_symmetric(matrix, tolerance) +% IS_SYMMETRIC Checks if a given matrix is symmetric within a specified tolerance. +% +% Syntax: +% isSymmetric = is_symmetric(matrix, tolerance) +% +% Input Arguments: +% matrix - A square matrix to be checked for symmetry. +% tolerance - A scalar value specifying the tolerance for the symmetry check. +% If not provided, the default value is 1e-15. +% +% Output Argument: +% isSymmetric - A logical value indicating whether the matrix is symmetric +% within the specified tolerance. Returns true if the matrix +% is symmetric and false otherwise. +% +% Example: +% matrix = [1, 2; 2, 1]; +% isSymmetric = is_symmetric(matrix, 1e-15); +% +% This will return true as the matrix is symmetric within the tolerance of 1e-15. +% +% Note: +% The function first checks if the matrix is square. If not, it returns false. +% Then it calculates the absolute difference between the matrix and its transpose. +% If all elements in the difference matrix are within the tolerance, it returns true. +% Otherwise, it returns false. +% +if nargin < 2 + tolerance = 1e-15; +if size(matrix, 1) ~= size(matrix, 2) + isSymmetric = false; + return; +end + +% Calculate the difference between the matrix and its transpose +diffMatrix = abs(matrix - matrix'); + +% Check if all elements in the difference matrix are within the tolerance +isSymmetric = all(all(diffMatrix <= tolerance)); +end \ No newline at end of file diff --git a/src/io/shrinkMat.m b/src/io/shrinkMat.m index c6c409f..1b2ae2f 100644 --- a/src/io/shrinkMat.m +++ b/src/io/shrinkMat.m @@ -38,7 +38,7 @@ else for i = 1:subjects cMat = data(:,:,i); % Select current matrix. - if ~issymmetric(cMat) + if ~is_symmetric(cMat) warning('Given matrix is not symmetric! Data index: %d', i) end edgeMat(i,:)=cMat(edgeIdx); % extract edge values. diff --git a/test/networks/networks/gen_BAnet.m b/test/networks/networks/gen_BAnet.m index e720e1e..17cfbc1 100644 --- a/test/networks/networks/gen_BAnet.m +++ b/test/networks/networks/gen_BAnet.m @@ -65,7 +65,7 @@ eIdx = eIdx + m*2; end -if ~issymmetric(g) +if ~is_symmetric(g) error('Network is not symmetric. Please run the function again.'); end