easy indexing lon-loop versus loop

Hello, I have what I think is an easy indexing question, that has me scratching my head. I set up a simple moving average as such:
% the MA depth
Ml=16;
% the input data
x=[ zeros(1,Ml) 1:10 9:-1:7 8:10 9:-1:7 8:10 9:-1:7 8:10 9:-1:7]; % sequence to average out
% storage for output
y= zeros(1,length(x)+Ml);
%index
n= [Ml+1:length(x)];
%moving average
y(n)= y(n-1) + x(n)/Ml - x(n-Ml)/Ml;
which gives the wrong value for y(n). If I implement as a loop, it works fine.
% storage for output
yl= zeros(1,length(x)+Ml);
for n= Ml+1:length(x)
yl(n)= yl(n-1) + x(n)/Ml - x(n-Ml)/Ml
end
what is wrong with the non-loop version?

1 件のコメント

stephen williams
stephen williams 2021 年 3 月 29 日
This is a general indexing question. I thought if you preallocated the y(n) locations, I could calculate y(n) based on y(n-1) but that is not working right.
I could have made up some other difference equation.

サインインしてコメントする。

回答 (1 件)

Bob Thompson
Bob Thompson 2021 年 3 月 29 日

0 投票

I think the issue is with your loop definition. You define y1 to be length(x)+M1 long, but n is defined from M1+1 to length(x). I assume you were intending to do 1+M1 to length(x)+M1, but you will need to add the M1 to both sides of the colon.
% storage for output
yl= zeros(1,length(x)+Ml);
for n= Ml+1:length(x)
yl(n)= yl(n-1) + x(n)/Ml - x(n-Ml)/Ml
end

4 件のコメント

stephen williams
stephen williams 2021 年 3 月 29 日
OK thanks. The issue I am trying to understand is with indexing in the non-loop equation.
%moving average
y(n)= y(n-1) + x(n)/Ml - x(n-Ml)/Ml;
If I add a line after to display each term, like this
[y(n-1)' (x(n)/Ml)' (-x(n-Ml)/Ml)' (y(n-1)+x(n)/Ml-x(n-Ml)/Ml)' y(n)']
you see this displayed
ans =
0 0.0625 0 0.0625 0.0625
0.0625 0.1250 0 0.1875 0.1250
0.1250 0.1875 0 0.3125 0.1875
0.1875 0.2500 0 0.4375 0.2500
.
.
.
stepping through line-by-line,
y(n-1)=0 looks right
x(n)/Ml= 0.0625 looks right
- x(n-Ml)/Ml=0 looks right
y(n-1)+x(n)/Ml-x(n-Ml)/Ml= 0.0625 looks right
y(n)= 0.0625 looks right
in the next line, n has incremented by 1
y(n-1)=0.0625 looks right
x(n)/Ml= 0.1250 looks right
- x(n-Ml)/Ml=0 looks right
y(n-1)+x(n)/Ml-x(n-Ml)/Ml= 0.1875 looks right
y(n)= 0.1250 ERROR- should have been 0.1875
in the next line, n has incremented by 1
y(n-1)=0.1250 ERROR- should have been 0.1875
x(n)/Ml= 0.1875 looks right
- x(n-Ml)/Ml=0 looks right
y(n-1)+x(n)/Ml-x(n-Ml)/Ml= 0.3125 looks right
y(n)= 0.1875 ERROR- should have been 0.3125
and so on.
This issue seems to have happened in the second iteration when the expression results were not saved in y(n) ? It looks like y(n) is in fact getting values from the 2nd term x(n)/Ml. How can that be?
Bob Thompson
Bob Thompson 2021 年 3 月 29 日
Oh, the issue is because you're trying to do all the calculations at once, so as far as the outside the loop calculations are concerned, the y(n-1) = 0 no matter what you want y(n-1) to be. To write it out a bit more orderly:
1) Define initial values of everything, M1, x(an array of values), y(zeros), n(an array of values).
2) Calculate y using [M1, x(an array of values), y(zeros), n(an array of values)].
The loop wouldn't run into this issue, because you're sequentially calculating each of the values of y, so y(n-1) will have a chance to be defined before y(n) gets calculated.
stephen williams
stephen williams 2021 年 4 月 1 日
thanks Bob.
Yes this issue is beucase I am trying to do the calculations all at once, and that is the question. I have aalternative ways to solve. I am trying to understand why this way does not work.
--Steve
stephen williams
stephen williams 2021 年 4 月 1 日
thanks Bob.
Yes this issue is beucase I am trying to do the calculations all at once, and that is the question. I have alternative ways to solve. I am trying to understand why this way does not work.
--Steve

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2021a

タグ

質問済み:

2021 年 3 月 29 日

コメント済み:

2021 年 4 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by