How to define a matrix which changes its size in every iteration of loop?

조회 수: 19 (최근 30일)
T = 50; %total iteration
x = randn(T,1); %input
pw =[0,0,0,0,0,1,-0.3,0.2]; % primary path TF
d = filter(pw, 1, x); % desiresd input
mu = 0.1; %step size
g = 1e-12; %gamma
%p = 2; %projection order
N = 8; %no of taps
Pmax = 4;
w = zeros(N,1); % weights of controller
y = zeros(1,T); % output of controller
%Xi = zeros(p,N); %input matrix
e_cont = zeros(T,1); %residual noise
p = zeros(T,1);
p(1)=Pmax;
for i=1:T
Xi(2:p(i),:) = Xi(1:p(i),:);
Xi(1,:) = [ x(i:-1:max(i-N+1,1)).',zeros(1,max(N-i,0)) ];
di = transpose([ d(i:-1:max(i-p(i)+1,1)).',zeros(1,max(p(i)-i,0)) ]);
Yi = Xi*w;
err= di-Yi;
sqe = err.^2;
UT = (mu*p(i)+2)*var/(2-mu);
LT = (mu*(p(i)-1)+2)*var/(2-mu);
if sqe(p(i)) > UT
p(i+1) = min(p(i)+1,Pmax);
elseif sqe(p(i))<= LT
p(i+1) = max(p(i)-1,1);
else
p(i+1) = p(i);
end
w = w + mu*Xi'*inv(g*eye(p(i))+Xi*Xi')*(di-Yi);
e_cont(i)= err(p(i));
end;
  댓글 수: 4
Stephen23
Stephen23 2021년 2월 23일
편집: Stephen23 2021년 2월 23일
Original question by zahid fazal retrieved from Google Cache:
How to define a matrix which changes its size in every iteration of loop?
T = 50; %total iteration
x = randn(T,1); %input
pw =[0,0,0,0,0,1,-0.3,0.2]; % primary path TF
d = filter(pw, 1, x); % desiresd input
mu = 0.1; %step size
g = 1e-12; %gamma
%p = 2; %projection order
N = 8; %no of taps
Pmax = 4;
w = zeros(N,1); % weights of controller
y = zeros(1,T); % output of controller
%Xi = zeros(p,N); %input matrix
e_cont = zeros(T,1); %residual noise
p = zeros(T,1);
p(1)=Pmax;
for i=1:T
Xi(2:p(i),:) = Xi(1:p(i),:);
Xi(1,:) = [ x(i:-1:max(i-N+1,1)).',zeros(1,max(N-i,0)) ];
di = transpose([ d(i:-1:max(i-p(i)+1,1)).',zeros(1,max(p(i)-i,0)) ]);
Yi = Xi*w;
err= di-Yi;
sqe = err.^2;
UT = (mu*p(i)+2)*var/(2-mu);
LT = (mu*(p(i)-1)+2)*var/(2-mu);
if sqe(p(i)) > UT
p(i+1) = min(p(i)+1,Pmax);
elseif sqe(p(i))<= LT
p(i+1) = max(p(i)-1,1);
else
p(i+1) = p(i);
end
w = w + mu*Xi'*inv(g*eye(p(i))+Xi*Xi')*(di-Yi);
e_cont(i)= err(p(i));
end;

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

채택된 답변

Walter Roberson
Walter Roberson 2021년 2월 20일
It is legal to change the size of a matrix inside a loop, including being legal to let it grow one element at a time every iteration. However, it is more efficient if you can create the matrix at full size ahead of time. Sometimes much more efficient.
The problem with your code is that you do not initialize the variable Xi but you have
Xi(2:p(i),:) = Xi(1:p(i),:);
That requires that rows 1 to Pmax and all columns of Xi are initialized before the statement is executed -- but Xi is undefined here.
Also, the right hand side has p(i)-1+1 = p(i) rows, but the left hand side has p(i)-2+1 = p(i)-1 rows . This is a mismatch: you would be trying to store 4 rows of data into a location that only holds three rows.
  댓글 수: 3
zahid fazal
zahid fazal 2021년 2월 20일
편집: zahid fazal 2021년 2월 20일
sir i tried to code for evolving order affine projection algorithm, which changes size of imput matrix (Xi) at every iteration acoording to p. what could be possible solution for it?
note: Xi is p rows N column matrix.
Walter Roberson
Walter Roberson 2021년 2월 20일
I have not heard of "evolving order affine projection".

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by