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 回表示 (過去 30 日間)
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 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 5 月 2 日
What should be the final output for a given n?
KOUSHIK
KOUSHIK 2023 年 5 月 2 日
If yS1:
-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 -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 1 -1 -1 -1 2 -1 2
-1 -1 -1 -1 1 -1 -1 -1 2 -1
S2:
-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 2 -1
-1 -1 -1 1 -1 -1 -1 2 -1 2
-1 -1 -1 -1 1 -1 -1 -1 2 -1
S3:
-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 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 0 -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 2
-1 -1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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
ou found the output of the above code it give me 7 matrices
and I need
S8
-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 0 -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

サインインしてコメントする。

採用された回答

Saffan
Saffan 2023 年 6 月 1 日
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 mat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 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 0 -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 0 -1 -1 -1 2 -1 2 -1 -1 -1 -1 0 -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
%% 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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeQuadratic Programming and Cone Programming についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by