I have written a simple code that performs a moving average smoothing algorithm (it averages n adjacent bins to a single bin similar to Matlab's smooth):
signal = ones(1,30); % Random sample
n=3; % An odd number (filter span)
for i = k : length(signal)-k
NewSignal(i,:) = sum(signal(i-n:i+n,:))/n;
end
However, the second to last line results in the following error:
%Index in position 1 is invalid. Array indices must be positive integers or logical values.
So, what is the cause of this error? And what would be a solution?
Any explanation would be greatly appreciated.

3 件のコメント

madhan ravi
madhan ravi 2019 年 5 月 19 日
signal(n-i:i+n,:)
% ^^^---- perhaps you meant this?
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 5 月 19 日
Perhaps reflects error of exceeds array bounds, in this case too.
madhan ravi
madhan ravi 2019 年 5 月 19 日
Didn't test while commenting.

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

 採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 5 月 19 日
編集済み: KALYAN ACHARJYA 2019 年 5 月 19 日

1 投票

signal(i-n:i+n,:)
%.......^ here is the issue
When you run the code n=3 initial and k=2;
When you assigned i=k:... that menas first value is 2
signal(2-3:2+3,:)...
Which trsult >> signal(-1:5,:)
%.......................^......
Negative and zero index value is not allowed. index value must be positive integer only as stated in error message-
Array indices must be positive integers or logical values. Soln: If you want to use the same expression >>Modify the initial value such a way that k>n, ensure that k value is integer.
Learn array indexing here

8 件のコメント

madhan ravi
madhan ravi 2019 年 5 月 19 日
signal(i-n:i+n,:) % isn't that the same in the OP's question @Kalyan?
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 5 月 19 日
編集済み: KALYAN ACHARJYA 2019 年 5 月 19 日
Sorry, Not getting sir @madhan
Sordin
Sordin 2019 年 5 月 19 日
編集済み: Sordin 2019 年 5 月 19 日
But that's not the issue. As you can see, I tweaked my code but I am still getting the same error. The loop stops when we have i = 2 and N = 1 (so the index i-N is a positive integer).
Sordin
Sordin 2019 年 5 月 19 日
Never mind, it did finally work. Thanks.
madhan ravi
madhan ravi 2019 年 5 月 19 日
Never mind Kalyan it was implied before you edited the answer.
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 5 月 19 日
Yes in that case also
The loop stops when we have i = 2 and N = 1 (so the index i-N is a positive integer>)
As I mentioned in @madhan ravi comment it reflects error of exceeds array bounds, in this case too.
Anyway, good to know that you resolved the issue.
Regards
madhan ravi
madhan ravi 2019 年 5 月 19 日
編集済み: madhan ravi 2019 年 5 月 19 日
Sordin: Could you post the solution ? As far as I understand are you trying to average the vector by n terms ?, if so this is achieved even without a loop.
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 5 月 19 日
編集済み: KALYAN ACHARJYA 2019 年 5 月 19 日
Commnent to @madhan ravi only today I have traced you in linkedIn. Great to see that you are too young. Keep it up and pls do continue to teach me in future too.
Gratitude to you!
Regards
Kalyan Acharjya

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

その他の回答 (0 件)

製品

リリース

R2019a

質問済み:

2019 年 5 月 19 日

編集済み:

2019 年 5 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by