How to define a matrix which changes its size in every iteration of loop?
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
zahid fazal
le 20 Fév 2021
Commenté : Rena Berman
le 6 Mai 2021
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 commentaires
Stephen23
le 23 Fév 2021
Modifié(e) : Stephen23
le 23 Fév 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;

Réponse acceptée
Walter Roberson
le 20 Fév 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!