I want Genetrate all possible breakage states. That is 1 become 0 but 1st,last row and 1st,last column are not going to change i.e they are allawaies 1.How can I do this? 1.
1 view (last 30 days)
Show older comments
My code is
n = 10; % Set value of n
State_Matrix = zeros(n);
s = transpose(1:n);
for i = 1:n
for j = 1:n
d = s(j) - s(i);
if d == (n/2) || d == -(n/2)
State_Matrix(i,j) = 1;
elseif d == 1 || d == -1
State_Matrix(i,j) = 2;
elseif i == n/2 && j == n/2-1 % add 010 state
State_Matrix(i,j) = 1;
else
State_Matrix(i,j) = -1; % set all other elements to -1
end
if (i == n/2 && j == n/2+1) || (i == n/2+1 && j == n/2)
State_Matrix(i,j) = -2; % set element to -2
end
end
end
% Generate all breakage states
MS = State_Matrix;
for i = 2:(n/2-1)
MS_i = Bond_Break(n,i,State_Matrix);
MS = [MS, MS_i];
end
% Store each matrix in a cell array with a meaningful name
matrices = {};
for i = 1:length(MS)/n
start_index = (i-1)*n+1;
end_index = i*n;
S_i = MS(:, start_index:end_index);
matrix_name = ['S', num2str(i)];
eval([matrix_name, ' = S_i;']);
matrices{i} = S_i;
end
% Display the number of matrices and each matrix
disp(['Number of matrices: ', num2str(length(matrices))]);
for i = 1:length(matrices)
matrix_name = ['S', num2str(i)];
disp([matrix_name, ':']);
disp(matrices{i});
end
%% Column No finding that contain zero
% column_No=[];
% for i=1:length(matrices)
% S_i=matrices{i};
% [row,column_i]=find(S_i==0);
% column_No=[column_No;column_i];
% end
%% Function to generate all breakage states for a given x value
function [S] =Break(n, x, State_Matrix)
S = [];
while x < n/2
State_Matrix(x, x+n/2) = 0;
State_Matrix(x+n/2, x) = 0;
S = [S, State_Matrix];
x = x + 1;
end
end
Now this give me 7 matrices but
-1 2 -1 -1 -1 1 -1 -1 -1 -1
2 -1 2 -1 -1 -1 0 -1 -1 -1
-1 2 -1 2 -1 -1 -1 1 -1 -1
-1 -1 2 -1 2 -1 -1 -1 0 -1
-1 -1 -1 2 -1 -2 -1 -1 -1 1
1 -1 -1 -1 -2 -1 2 -1 -1 -1
-1 1 -1 -1 -1 2 -1 2 -1 -1
-1 -1 1 -1 -1 -1 2 -1 2 -1
-1 -1 -1 0 -1 -1 -1 2 -1 2
-1 -1 -1 -1 1 -1 -1 -1 2 -1
This matrix is missing how can I do this from here
2 Comments
Accepted Answer
Saffan
on 1 Jun 2023
Hi Koushik,
The current implementation of Bond_Break function does not incorporate all possible combinations, and therefore, it may not generate all potential breakage states. In the Bond_Break function, first get the list of all the points and then get all possible combinations of these points.
You can modify your code and Bond_Break function in the following way:
n = 10; % Set value of n
State_Matrix = zeros(n);
s = transpose(1:n);
for i = 1:n
for j = 1:n
d = s(j) - s(i);
if d == (n/2) || d == -(n/2)
State_Matrix(i,j) = 1;
elseif d == 1 || d == -1
State_Matrix(i,j) = 2;
elseif i == n/2 && j == n/2-1 % add 010 state
State_Matrix(i,j) = 1;
else
State_Matrix(i,j) = -1; % set all other elements to -1
end
if (i == n/2 && j == n/2+1) || (i == n/2+1 && j == n/2)
State_Matrix(i,j) = -2; % set element to -2
end
end
end
% Generate all breakage states
MS = State_Matrix;
MS_temp = Bond_Break(n,2,State_Matrix);
MS = [MS, MS_temp];
% Store each matrix in a cell array with a meaningful name
matrices = {};
for i = 1:length(MS)/n
start_index = (i-1)*n+1;
end_index = i*n;
S_i = MS(:, start_index:end_index);
matrix_name = ['S', num2str(i)];
eval([matrix_name, ' = S_i;']);
matrices{i} = S_i;
end
% Display the number of matrices and each matrix
disp(['Number of matrices: ', num2str(length(matrices))]);
for i = 1:length(matrices)
matrix_name = ['S', num2str(i)];
disp([matrix_name, ':']);
disp(matrices{i});
end
%% Column No finding that contain zero
% column_No=[];
% for i=1:length(matrices)
% S_i=matrices{i};
% [row,column_i]=find(S_i==0);
% column_No=[column_No;column_i];
% end
%% Function to generate all breakage states for a given x value
function [S] = Bond_Break(n, x, State_Matrix)
S = [];
Points = {};
% Storing all points where breakage will occur
while x < n/2
p = [x,x+n/2];
Points{end+1} = p;
x = x + 1;
end
% Generating all possible combinations of these points:
combos = cell(length(Points), 1);
for ii = 1:length(Points)
combos{ii} = {[Points{ii}]};
end
for i = 2:length(Points)
C = combnk(1:length(Points), i);
for ii = 1:size(C, 1)
combo = [];
for jj = 1:i
combo{end+1} = Points{C(ii,jj)};
end
combos{end+1} = {combo};
end
end
%Adding matrices to S:
for i=1:length(combos)
SM = State_Matrix;
if(i<=length(Points))
p1=combos{i}{1}(1);
p2=combos{i}{1}(2);
SM(p1,p2)=0;
SM(p2,p1)=0;
else
for j=1:length(combos{i}{1})
p1=combos{i}{1}{j}(1);
p2=combos{i}{1}{j}(2);
SM(p1,p2)=0;
SM(p2,p1)=0;
end
end
S = [S,SM];
end
end
More Answers (0)
See Also
Categories
Find more on Quadratic Programming and Cone Programming 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!