How to update a symmetric matrix?
3 views (last 30 days)
Show older comments
I want to update a symmetric matrix values using Particle Swarm Optimization, I mean the final updated matrix should be kept symmertic and maintaind the zero values as it is without changing it?
This is the matrix that I want to update without changing the zero values and keep it symmetric:
M= [1 0 1 1 0 0 0 0 0 0 0 0
0 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 1 0 0 0 0 0 0
1 1 1 1 1 1 0 0 0 0 0 0
0 0 1 1 1 1 1 1 0 0 0 0
0 0 1 1 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 0 1 1 0 0
0 0 0 0 1 1 0 1 1 1 0 0
0 0 0 0 0 0 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1
0 0 0 0 0 0 0 0 1 1 1 1
0 0 0 0 0 0 0 0 1 1 1 1]
And this is the part of the code that I can put the constraints on the updating values on variable min and variable max, the range I want from 1 to 100,
% Project Code: YPEA102
% Project Title: Implementation of Particle Swarm Optimization in MATLAB
% Publisher: Yarpiz (www.yarpiz.com)
% Developer: S. Mostapha Kalami Heris (Member of Yarpiz Team)
% Contact Info: sm.kalami@gmail.com, info@yarpiz.com
function [x,err,B1]=pso(CostFunction)
% CostFunction= Cost Function
% nVar= Number of Decision Variables
B1=[];
nVar = 12;
VarSize=[nVar 12]; % Size of Decision Variables Matrix
VarMin= 1; % Lower Bound of Variables
VarMax= 100; % Upper Bound of Variables
9 Comments
Torsten
on 2 May 2023
I don't know exactly what you intend to do. I thought you get an input matrix M and want to return a randomly modified symmetric matrix as described above.
So generate a matrix of zeros of the same size as M, put random integers between VarMin and VarMax at the nonzero locations on and below the main diagonal and use this link
to copy the lower part of the matrix to the upper part.
Accepted Answer
James Tursa
on 2 May 2023
E.g.,
M = rand(6)<0.3; M = double(logical(M+M')) % an arbitrary symmetric matrix
VarMin= 1; % Lower Bound of Variables
VarMax= 100; % Upper Bound of Variables
N = size(M,1); % number of elements in a dimension
T1 = logical(triu(ones(N),1)) % for picking off the upper off-diagonals
X = logical(M); % location of the 1's
U = X & T1 % location of the 1's on upper off-diagonal
nU = nnz(U); % how many of them?
vU = randi([VarMin,VarMax],[1,nU]); % generate the random integers
R = zeros(N); % the result matrix
R(U) = vU % set the upper off-diagonals
R = R + R' % copy them to lower
D = diag(X); % location of diagonal 1's
nD = nnz(D); % how many of them?
vD = randi([VarMin,VarMax],[1,nD]); % generate the random integers
D = diag(D); % turn D back onto a matrix
R(D) = vD % set the diagonal elements
4 Comments
More Answers (0)
See Also
Categories
Find more on Sparse Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!