How to update a symmetric matrix?

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

We need more detail. I don't see M anywhere in your code. Do you have current code that is making M not symmetric? Then show us what this is.
M
M on 2 May 2023
Edited: M on 2 May 2023
@James Tursa we can put constraints on the 12*12 values of matrix M to maintain the zero values and the symmetry by these lines:
nVar = 12;
VarSize=[nVar 12]; % Size of Decision Variables Matrix
VarMin= 1; % Lower Bound of Variables
VarMax= 100; % Upper Bound of Variables
Torsten
Torsten on 2 May 2023
Edited: Torsten on 2 May 2023
So the 1's in your 12x12 matrix M should be replaced by some random integers in between 1 and 100 while keeping the matrix symmetric ? Or what are you asking for ?
for example putting the VarMax and VarMin as the following, but how can I maintain the symmetry when updaing the values???
VarMin = [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]
VarMax= [100 0 100 100 0 0 0 0 0 0 0 0
0 100 100 100 0 0 0 0 0 0 0 0
100 100 100 100 100 100 0 0 0 0 0 0
100 100 100 100 100 100 0 0 0 0 0 0
0 0 100 100 100 100 100 100 0 0 0 0
0 0 100 100 100 100 100 100 0 0 0 0
0 0 0 0 100 100 100 0 100 100 0 0
0 0 0 0 100 100 0 100 100 100 0 0
0 0 0 0 0 0 100 100 100 100 100 100
0 0 0 0 0 0 100 100 100 100 100 100
0 0 0 0 0 0 0 0 100 100 100 100
0 0 0 0 0 0 0 0 100 100 100 100]
M
M on 2 May 2023
Edited: M on 2 May 2023
@Torsten exactly after N number of iterations, "the 1's in your 12x12 matrix should be replaced by some random integers in between 1 and 100 while keeping the matrix symmetric"
Torsten
Torsten on 2 May 2023
Edited: Torsten on 2 May 2023
In a first step, only change values below or above the main diagonal of the matrix.
After this, replace the values in the other half of the matrix by the corresponding values from the changes of the first step.
M
M on 2 May 2023
Edited: M on 2 May 2023
Do you mean in the PSO above code only put the upper values varibles, then in the main program copy it to the lower values, but what about the diagonal? make seperate variables to it?
Also, is there a command to clone the upper values to the lower values?
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.
@Torsten Thanks for your suggestion

Sign in to comment.

 Accepted Answer

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 Comments

@James Tursa suppose from this matrix
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
how to select the upper part and the diagonal, then clone the upper to the lower?
@M - learn to use the tools in MATLAB. In this case, how do you select the upper triangle?
help triu
TRIU Extract upper triangular part. TRIU(X) is the upper triangular part of X. TRIU(X,K) is the elements on and above the K-th diagonal of X. K = 0 is the main diagonal, K > 0 is above the main diagonal and K < 0 is below the main diagonal. See also TRIL, DIAG. Documentation for triu doc triu Other uses of triu codistributed/triu gpuArray/triu sym/triu symbolic/triu
R = [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];
Extract the upper triangle.
Tu = triu(R,1)
Tu = 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
D = diag(diag(R))
D = 6×6
0 0 0 0 0 0 0 0 0 0 0 0 0 0 75 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 38
Now, create a new matrix from these parts.
Q = Tu + D + Tu'
Q = 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
R was already symmetric, so we should have Q==R.
Simpler:
U = triu(X,1);
No need for T1.

Sign in to comment.

More Answers (0)

Categories

Asked:

M
M
on 2 May 2023

Commented:

on 3 May 2023

Community Treasure Hunt

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

Start Hunting!