How to update a symmetric matrix?

조회 수: 1 (최근 30일)
M
M 2023년 5월 2일
댓글: James Tursa 2023년 5월 3일
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.
M
M 2023년 5월 2일
@Torsten Thanks for your suggestion

댓글을 달려면 로그인하십시오.

채택된 답변

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
M
M 2023년 5월 2일
James Tursa
James Tursa 2023년 5월 3일
Simpler:
U = triu(X,1);
No need for T1.

댓글을 달려면 로그인하십시오.

추가 답변 (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