How to run a code several times using a for loop

3 views (last 30 days)
Hello,
I have the following problem: I want to run the following code for 50 interations, and saving the values of matrix A1, B1, C1 ,D1 , OutputL_1, OutputH1_1, OutputH1_2, OutputH1_3, ........, OutputL_20, OutputH1_20, OutputH2_20, OutputH3_20. I tried through for-loop but I do not have the desired result. Below I have the basic parts of the code. Your help is important!!!
Datay=Dt(1:24)
Noutput=Out %Noutput: Matrix: 1X24
% The following produce must be performed for 50 iterations
E=Datay-Noutput;
%Create 20 subsets
Esubset1=E(randi([1,numel(E)],size(E)));
Esuibset2=E(randi([1,numel(E)],size(E)));
.
.
Esuibset20=E(randi([1,numel(E)],size(E)));
% Create New Data
NewDataY1=Noutput+Esubset1;
NewDataY2=Noutput+Esubset2;
.
.
NewDataY20=Noutput+Esubset20;
% Processing
[C1,L1]=wavedec(NewDataY1,3,'db3');
[C2,L2]=wavedec(NewDataY2,3,'db3');
.
.
[C20,L20]=wavedec(NewDataY20,3,'db3');
LowSerCoef_1=wrcoef('a',C1,L1,'db3',3);
High1SerCoef_1=wrcoef('d',C1,L1,'db3',1);
High2SerCoef_1=wrcoef('d',C1,L1,'db3',2);
High3SerCoef_1=wrcoef('d',C1,L1,'db3',3);
.
.
LowSerCoef_20=wrcoef('a',C20,L20,'db3',3);
High1SerCoef_20=wrcoef('d',C20,L20,'db3',1);
High2SerCoef_20=wrcoef('d',C20,L20,'db3',2);
High3SerCoef_20=wrcoef('d',C20,L20,'db3',3);
% W matrices
LowSerW=rand(100,1000);
High1SerW=rand(100,1000);
High2SerW=rand(100,1000);
High3SerW=rand(100,1000);
LowSer2W=LowSerW*LowSerCoef;
High1Ser2W=High1SerW*High1SerCoef;
High2Ser2W=High2SerW*High2SerCoef;
High3Ser2W=High3SerW*High3SerCoef;
A=LowSer2W+z % z is 1X1000 matrix
B=High1Ser2W+z; % z is 1X1000 matrix
C=High2Ser2W+z; % z is 1X1000 matrix
D=High3Ser2W+z; % z is 1X1000 matrix
A1=1/(1+A);
B1=1/(1+B);
C1=1/(1+C);
D1=1/(1+D);
OutputL_1=A1*LowSerCoef_1;
OutptuH1_1=B1*High1SerCoef_1;
OutptuH1_1=C1*High2SerCoef_1;
OutptuH1_1=D1*High3SerCoef_1;
.
.
OutputL_20=A1*LowSerCoef_120
OutptuH1_20=B1*High1SerCoef_20;
OutptuH1_20=C1*High2SerCoef_20;
OutptuH1_20=D1*High3SerCoef_20;
  1 Comment
Stephen23
Stephen23 on 1 Jul 2020
Numbering variable names is a sign that you are doing something wrong.
Copy-and-pasting code like that is a sign that you are doing something wrong.
Your current approach is not efficient nor a particularly good use of MATLAB: arrays and indexing would be much better.

Sign in to comment.

Answers (2)

KSSV
KSSV on 1 Jul 2020
  1. Make your given code into a function with input and output.
  2. Output should be your required matrices/ data.
  3. Make output into a single cell/ structure inside the function.
  4. Now run a loop and save each output into a cell.

Shae Morgan
Shae Morgan on 10 Aug 2020
Something like this?
Datay=rand(1,24)
Noutput=rand(1,24) %Noutput: Matrix: 1X24
% The following produce must be performed for 50 iterations
E=Datay-Noutput;
%Create 20 subsets
for i=1:20
Esubset(i,:)=E(randi([1,numel(E)],size(E)));
NewDataY(i,:)=Noutput+Esubset(i,:)
% Processing
[C(i,:),L(i,:)]=wavedec(NewDataY1,3,'db3');
LowSerCoef(i,:)=wrcoef('a',C(i,:),L(i,:),'db3',3);
High1SerCoef(i,:)=wrcoef('d',C1(i,:),L1(i,:),'db3',1);
High2SerCoef(i,:)=wrcoef('d',C1(i,:),L1(i,:),'db3',2);
High3SerCoef(i,:)=wrcoef('d',C1(i,:),L1,(i,:)'db3',3);
end
% W matrices
LowSerW=rand(100,1000);
High1SerW=rand(100,1000);
High2SerW=rand(100,1000);
High3SerW=rand(100,1000);
LowSer2W=LowSerW*LowSerCoef;
High1Ser2W=High1SerW*High1SerCoef;
High2Ser2W=High2SerW*High2SerCoef;
High3Ser2W=High3SerW*High3SerCoef;
A=LowSer2W+z % z is 1X1000 matrix
B=High1Ser2W+z; % z is 1X1000 matrix
C=High2Ser2W+z; % z is 1X1000 matrix
D=High3Ser2W+z; % z is 1X1000 matrix
A1=1/(1+A);
B1=1/(1+B);
C1=1/(1+C);
D1=1/(1+D);
for i=1:20
OutputL(i,:)=A1*LowSerCoef(i,:);
OutptuH1(i,:)=B1*High1SerCoef(i,:);
OutptuH1(i,:)=C1*High2SerCoef(i,:);
OutptuH1(i,:)=D1*High3SerCoef(i,:);
end

Tags

Community Treasure Hunt

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

Start Hunting!