How to simplify this code?

조회 수: 1 (최근 30일)
Eric Chua
Eric Chua 2020년 6월 8일
댓글: Eric Chua 2020년 6월 8일
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
KSSV
KSSV 2020년 6월 8일
What is X?
Eric Chua
Eric Chua 2020년 6월 8일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

KSSV
KSSV 2020년 6월 8일
편집: KSSV 2020년 6월 8일
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
Eric Chua
Eric Chua 2020년 6월 8일
I think i got it already thank you very much.
Eric Chua
Eric Chua 2020년 6월 8일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by