How to update a symmetric matrix?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Torsten
2023-5-2
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.
采纳的回答
James Tursa
2023-5-2
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 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!