Generate all possible combinations of input vector Then store the input and outputs in each iteration in .mat file

1 次查看(过去 30 天)
In the following code I want to generate all probability of being 0.5 in vector 'Pd' .
For example:
In the first iteration Pd will be as the following : Pd=[0.5 1 1 1 1 1 1]'
In the second iteration Pd will be as the following : Pd=[1 0.5 1 1 1 1 1]'
.
.
.
In i number of iteration Pd will be as the following : Pd=[1 0.5 0.5 0.5 1 1 1]'
In i number of iteration Pd will be as the following : Pd=[0.5 0.5 0.5 0.5 0.5 0.5 0.5]'
And so on ... (I want all possible combinations)
Then I want to store 'Pd' vector , 'Dd' matrix and 'w' vector in file which is named 'Comb#number of itertaion' (i.e , Comb1 , Comb2 ... ) in each iteration.
This is a part from the code:
Ph=[1 1 1 1 1 1 1]' ;
Pd=[0.5 1 1 1 1 1 1]' ;
Kd=zeros(14) ; Kh=zeros(14) ;
for i=1:7 ;
Ki=build_beam(i) ;
Kd=Kd+Pd(i)*Ki ;
Kh=Kh+Ph(i)*Ki ;
end ;
a=1:2:14;
r=7 ;
[Vd,Dd]=eig(Kd,M) ;
Dd=diag(Dd) ;
[Dd,I]=sort(Dd) ; Dd=Dd(1:vibmodes) ;
Vd=Vd(:,I); Td=Vd(:,1:vibmodes) ; Td=Td(a,:) ;
Dd=diag(Dd) ;
w=sqrt((diag(Dh)));

采纳的回答

Matt J
Matt J 2022-3-4
编辑:Matt J 2022-3-4
Well, here are all the combinations. You can loop through the rows of Pd and save them after whatever processing you wish. You've seen examples of similar things in previous posts. However, I think it's probably a bad idea to store the results to separate .mat files. You should probably store them all in one matfile as arrays.
[Pd{1:7}]=ndgrid([0.5,1]);
Pd=reshape( cat(8,Pd{:}) ,[],7)
Pd = 128×7
0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 1.0000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 1.0000 0.5000 0.5000 0.5000 0.5000 0.5000 1.0000 1.0000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 1.0000 0.5000 0.5000 0.5000 0.5000 1.0000 0.5000 1.0000 0.5000 0.5000 0.5000 0.5000 0.5000 1.0000 1.0000 0.5000 0.5000 0.5000 0.5000 1.0000 1.0000 1.0000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 1.0000 0.5000 0.5000 0.5000 1.0000 0.5000 0.5000 1.0000 0.5000 0.5000 0.5000
  6 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by