How to simplify this code?

1 visualización (últimos 30 días)
Eric Chua
Eric Chua el 8 de Jun. de 2020
Comentada: Eric Chua el 8 de Jun. de 2020
X16 = [X{3}',X{4}',X{5}',X{6}',X{7}',X{8}',X{9}',X{10}',X{11}',X{12}',X{13}',X{14}',X{15}',X{16}'];
% X32 = [X[3]' to X[32]'];
% X48 = [X{3}' to X[48}'];
% X64 = [X{3}' to X{64}'];
% X80 = [X{3}' to X{80}'];
% X96 = [X{3}' to X{96}'];
% X112 = [X{3}' to X{112}'];
% X128 = [X{3}' to X{128}'];
% X144 = [X{3}' to X{144}'];
% X160= [X{3}' to X{160}'];
Hi, lets say i have already defined my X{3} to X{160}, how do i define my X32, X48, X64, X80, X96, X112, X128, X144, and X160 without writing one by one?
  2 comentarios
KSSV
KSSV el 8 de Jun. de 2020
What is X?
Eric Chua
Eric Chua el 8 de Jun. de 2020
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] ;
x11 = [x1 x1 x1 x1 x1 x1 x1 x1 x1 x1];
x22 = [x2 x2 x2 x2 x2 x2 x2 x2 x2 x2];
L = 3;
x = zeros(160,2)
for i=3:160
x(i,:) = [x11(i) x22(i)]
end
X = cell(160,1)
X{3} = [x11(3) x22(3) x11(2) x22(2) x11(1) x22(1) 1]
for i=3:160
X{i} = [x11(i) x22(i) x11(i-1) x22(i-1) x11(i-L+1) x22(i-L+1) 1]
end
%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)
%
X16 = [X{3}',X{4}',X{5}',X{6}',X{7}',X{8}',X{9}',X{10}',X{11}',X{12}',X{13}',X{14}',X{15}',X{16}'];
X32 = [X{3}' to X{32}'];
% X48 = [X{3} to X[48}];
% X64 = [X{3} to X{64}];
% X80 = [X{3} to X{80}];
% X96 = [X{3} to X{96}];
% X112 = [X{3} to X{112}];
% X128 = [X{3} to X{128}];
% X144 = [X{3} to X{144}];
% X160= [X{3} to X{160}];
This is my whole code im trying out.

Iniciar sesión para comentar.

Respuesta aceptada

KSSV
KSSV el 8 de Jun. de 2020
Editada: KSSV el 8 de Jun. de 2020
X = 1:10 ;
X10 = zeros([],1) ;
for i = 3:10
X10 = [X10 X(i)] ;
end
The above can be simply achieved using:
X10 = X(3:10) ;
  2 comentarios
Eric Chua
Eric Chua el 8 de Jun. de 2020
I think i got it already thank you very much.
Eric Chua
Eric Chua el 8 de Jun. de 2020
N = 1000;
MSE = zeros(N,2) ;
for i = 1:N
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] ;
x11 = [x1 x1 x1 x1 x1 x1 x1 x1 x1 x1];
x22 = [x2 x2 x2 x2 x2 x2 x2 x2 x2 x2];
L = 3;
x = zeros(160,2);
for i=3:160
x(i,:) = [x11(i) x22(i)];
end
X = cell(160,1);
X{3} = [x11(3) x22(3) x11(2) x22(2) x11(1) x22(1) 1];
for j=3:160
X{j} = [x11(j) x22(j) x11(j-1) x22(j-1) x11(j-L+1) x22(j-L+1) 1]';
end
%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);
X16 = [X{3},X{4},X{5},X{6},X{7},X{8},X{9},X{10},X{11},X{12},X{13},X{14},X{15},X{16}];
%C, a 7x2 matrjx
C = [r1; r2]' ;
%Y, a 14x2 matrjx
Y16 = X16'*C ;
%Yd = Pojss(Y) (at equatjon (8))
Yd16 = poissrnd(Y16);
%Least Square Estimate of C
Cls16 = (inv(X16*X16'))*(X16*Yd16);
% To set to zero all the negative entries of C
Cls016 = max(Cls16,0);
%Mean square error of LS C and C
MSE16(j,:) = mean((C - Cls016).^2) ;
end
MSEdB16 = 20*log10(MSE16(j,:))
I run the code for 1000 times, how to code for the average of my MSE value? For example for n=1, mse = A, for n=2, mse = B up to N then divide by N

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by