"Index in position 1 exceeds array bounds" error for loop

1 回表示 (過去 30 日間)
Atiqah Zakirah
Atiqah Zakirah 2019 年 7 月 20 日
編集済み: Rik 2019 年 7 月 20 日
I'm trying to determine if there is a change in sign between the current and next value in my array (size is 2000 x 131) and count the number of times there is a change using a for loop as shown in the following code. The error lies in line "nextVal = brakingData(j+1,k)". I can't understand why this is an error. Why is the inital value of j 2000 when I run the loop? Shouldn't it be 1? How do I go about solving this error?
for k = 1:size(brakingData,2) %131
zeroCrossCount = 0;
for j = 1:length(brakingData) %2000
currentVal = brakingData(j,k);
nextVal = brakingData(j+1,k);
if ~isequal(sign(currentVal), sign(nextVal))
zeroCrossCount = zeroCrossCount + 1;
else
zeroCrossCount = zeroCrossCount + 0;
end
end
brakingFreqValue = zeroCrossCount/20;
brakingFreq(:,k) = brakingFreqValue;
end

回答 (1 件)

Rik
Rik 2019 年 7 月 20 日
編集済み: Rik 2019 年 7 月 20 日
Instead of this line
for j = 1:length(brakingData)
Use this:
for j = 1:(size(brakingData,1)-1)
Although you can replace the loop with an array operation.

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by