compute sums by accumulating in a for-loop
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I am trying to accumulate sums (phi(l) for lags " l"in a for loop given the code below. I keep getting "Unable to perform assignment because the left and right sides have a different number of elements." I have tried multiple ways to try and fix it but nothing is working.
x=normrnd(0,1,100,1);
xprime=detrend(x,'constant');
phi=zeros(14,1);
L= [0:13]';
for l=1:14
phi(l)=(sum(xprime(1:100-l)).*xprime(1+l:100)/(100-(l-1)-1));
end
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 3 Dez. 2023
Look at this:
x=normrnd(0,1,100,1);
xprime=detrend(x,'constant');
phi=zeros(14,1);
L= [0:13]';
for l=1:14
temp = (sum(xprime(1:100-l)).*xprime(1+l:100)/(100-(l-1)-1))
fprintf('The size of temp = %d.\n', numel(temp))
phi(l)=(sum(xprime(1:100-l)).*xprime(1+l:100)/(100-(l-1)-1));
end
So you're trying to stuff 99 values into a slot meant for only one value, phi(l). Not sure how to fix it because I'm not sure what your intent is. Did you mean for phi to be a 99 by 14 matrix and you want to put temp into the columns of phi?
3 Kommentare
Image Analyst
am 3 Dez. 2023
Watch your parentheses. Maybe you meant either
phi(l) = sum(xprime(1:(100-l)) .* xprime(l:100) / (100-(l-1)-1));
or
phi(l) = sum(xprime(1:(100-l) .* xprime(l:100)) / (100-(l-1)-1);
And l (ell) is not a good variable name - it looks too much like 1 (one) and I (capital I). Use k instead.
Weitere Antworten (1)
Torsten
am 3 Dez. 2023
Bearbeitet: Torsten
am 3 Dez. 2023
sum(xprime(1:100-l))
This is a scalar.
sum(xprime(1:100-l)).*xprime(1+l:100)
This is a vector of length 100-(1+l)+1.
(sum(xprime(1:100-l)).*xprime(1+l:100)/(100-(l-1)-1))
This remains a vector of length 100-(1+l)+1.
phi(l)
This is s acalar.
Thus you try to assign a vector to a scalar which is not possible.
Maybe you mean
phi(l)=sum(xprime(1:100-l).*xprime(1+l:100))/(100-(l-1)-1));
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!