How to run this code for 1000 times?

%For C1
lambda1 = [60.21, 41.58, 9.11, 8.71, 3.83, 3.74, 18.06]
r1 = poissrnd(lambda1)
%For C2
lambda2 = [41.58, 60.21, 8.71, 9.11, 3.74, 3.83, 18.06]
r2 = poissrnd(lambda2)
%Designed training sequences x1 and x2
x1 = [1,1,1,0,0,0,0,1,0,1,0,1,1,0,0,1] ;
x2 = [1,1,1,0,1,0,0,0,1,1,1,0,0,0,0,1] ;
%X[3] to X[16]
X3 = [1 1 1 1 1 1 1]' ;
X4 = [0 0 1 1 1 1 1]' ;
X5 = [0 1 0 0 1 1 1]' ;
X6 = [0 0 0 1 0 0 1]' ;
X7 = [0 0 0 0 0 1 1]' ;
X8 = [1 0 0 0 0 0 1]' ;
X9 = [0 1 1 0 0 0 1]' ;
X10 = [1 1 0 1 1 0 1]' ;
X11 = [0 1 1 1 0 1 1]' ;
X12 = [1 0 0 1 1 1 1]' ;
X13 = [1 0 1 0 0 1 1]' ;
X14 = [0 0 1 0 1 0 1]' ;
X15 = [0 0 0 0 1 0 1]' ;
X16 = [1 1 0 0 0 0 1]' ;
%X,a 7x14 matrix
X = [X3,X4,X5,X6,X7,X8,X9,X10,X11,X12,X13,X14,X15,X16];
%C, a 7x2 matrix
C = [r1; r2]' ;
%Y, a 14x2 matrix
Y = X'*C ;
%Yd = Poiss(Y) (at equation (8))
Yd = poissrnd(Y)
%y1, a 14x1 matrix ; y2, a 14x1 matrix
y1 = Y(:,1)
y2 = Y(:,2)
%Least Square Estimate of C
Cls = (inv(X*X'))*(X*Yd)
% To set to zero all the negative entries of C
Cls1 = max(Cls,0)
%Mean square error of LS C and C
MSE = mean((C - Cls1).^2)

4 comentarios

Eric Chua
Eric Chua el 27 de Mayo de 2020
I need to get the mean square error (MSE) for 1000 realization, how should i code it in a simpler way? Any help is appreciated.
Subhadeep Koley
Subhadeep Koley el 27 de Mayo de 2020
@Eric Chua Which parameter will be changed in every iteration?
Eric Chua
Eric Chua el 27 de Mayo de 2020
Thanks for the reply. r1 and r2 are values that will change every single run as they are generated with poisson, and so do the C, Y, Yd.
Eric Chua
Eric Chua el 27 de Mayo de 2020
Cls and Cls1 are also changing for every run.

Iniciar sesión para comentar.

 Respuesta aceptada

KSSV
KSSV el 27 de Mayo de 2020
N = 1000 ;
MSE = zeros(N,2) ;
for i = 1:N
%For C1
lambda1 = [60.21, 41.58, 9.11, 8.71, 3.83, 3.74, 18.06] ;
r1 = poissrnd(lambda1) ;
%For C2
lambda2 = [41.58, 60.21, 8.71, 9.11, 3.74, 3.83, 18.06] ;
r2 = poissrnd(lambda2) ;
%Designed training sequences x1 and x2
x1 = [1,1,1,0,0,0,0,1,0,1,0,1,1,0,0,1] ;
x2 = [1,1,1,0,1,0,0,0,1,1,1,0,0,0,0,1] ;
%X[3] to X[16]
X3 = [1 1 1 1 1 1 1]' ;
X4 = [0 0 1 1 1 1 1]' ;
X5 = [0 1 0 0 1 1 1]' ;
X6 = [0 0 0 1 0 0 1]' ;
X7 = [0 0 0 0 0 1 1]' ;
X8 = [1 0 0 0 0 0 1]' ;
X9 = [0 1 1 0 0 0 1]' ;
X10 = [1 1 0 1 1 0 1]' ;
X11 = [0 1 1 1 0 1 1]' ;
X12 = [1 0 0 1 1 1 1]' ;
X13 = [1 0 1 0 0 1 1]' ;
X14 = [0 0 1 0 1 0 1]' ;
X15 = [0 0 0 0 1 0 1]' ;
X16 = [1 1 0 0 0 0 1]' ;
%X,a 7x14 matrix
X = [X3,X4,X5,X6,X7,X8,X9,X10,X11,X12,X13,X14,X15,X16];
%C, a 7x2 matrix
C = [r1; r2]' ;
%Y, a 14x2 matrix
Y = X'*C ;
%Yd = Poiss(Y) (at equation (8))
Yd = poissrnd(Y) ;
%y1, a 14x1 matrix ; y2, a 14x1 matrix
y1 = Y(:,1) ;
y2 = Y(:,2) ;
%Least Square Estimate of C
Cls = (inv(X*X'))*(X*Yd) ;
% To set to zero all the negative entries of C
Cls1 = max(Cls,0) ;
%Mean square error of LS C and C
MSE(i,:) = mean((C - Cls1).^2) ;
end

11 comentarios

Eric Chua
Eric Chua el 27 de Mayo de 2020
Thanks for the help!
KSSV
KSSV el 27 de Mayo de 2020
Thanks is accepting the asnwer.
Eric Chua
Eric Chua el 27 de Mayo de 2020
May I know how to check for the run to make sure it is correct? For example I need only the mse of third run.
KSSV
KSSV el 27 de Mayo de 2020
If you see MSE is vector of size N*2 i.e 1000x2. If you want MSE for 3rd iteration, use:
MSE(3,:)
Eric Chua
Eric Chua el 27 de Mayo de 2020
Thanks for the reply, what is the code for plotting this MSE graph against something?
KSSV
KSSV el 27 de Mayo de 2020
plot(MSE(:,1))
Eric Chua
Eric Chua el 27 de Mayo de 2020
Does the (: , 1) here mean first column of the MSE?
KSSV
KSSV el 27 de Mayo de 2020
Yes man...you should read this basic stuff...
MSE(:,1) % first column
MSE(:,2) % second column
Eric Chua
Eric Chua el 27 de Mayo de 2020
Thank you very much, that help me alot.
Eric Chua
Eric Chua el 28 de Mayo de 2020
How do I add up all the MSE value in first column and divide by 1000 to get the average?
KSSV
KSSV el 29 de Mayo de 2020
Use the function mean.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 27 de Mayo de 2020

Comentada:

el 29 de Mayo de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by