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
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
James Tursa 2023-5-2
E.g.,
M = rand(6)<0.3; M = double(logical(M+M')) % an arbitrary symmetric matrix
M = 6×6
0 1 1 1 0 0 1 0 1 0 0 1 1 1 1 1 1 1 1 0 1 0 0 0 0 0 1 0 0 0 0 1 1 0 0 1
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
T1 = 6×6 logical array
0 1 1 1 1 1 0 0 1 1 1 1 0 0 0 1 1 1 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0
X = logical(M); % location of the 1's
U = X & T1 % location of the 1's on upper off-diagonal
U = 6×6 logical array
0 1 1 1 0 0 0 0 1 0 0 1 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
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 = 6×6
0 35 66 37 0 0 0 0 11 0 0 47 0 0 0 23 74 26 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
R = R + R' % copy them to lower
R = 6×6
0 35 66 37 0 0 35 0 11 0 0 47 66 11 0 23 74 26 37 0 23 0 0 0 0 0 74 0 0 0 0 47 26 0 0 0
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
R = 6×6
0 35 66 37 0 0 35 0 11 0 0 47 66 11 75 23 74 26 37 0 23 0 0 0 0 0 74 0 0 0 0 47 26 0 0 38
  4 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Sparse Matrices 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by