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

21 views (last 30 days)
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 Comments
Stephen23
Stephen23 on 23 Feb 2021
Edited: Stephen23 on 23 Feb 2021
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;

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 20 Feb 2021
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 Comments
zahid fazal
zahid fazal on 20 Feb 2021
Edited: zahid fazal on 20 Feb 2021
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!