Index exceeds the number of array elements (1264).
1 回表示 (過去 30 日間)
古いコメントを表示
I have been trying to work on a function however it continues to get stuck on this section. I have made sure that the matrix/vector isn't starting on zero, but I dont understand why I continue to get this error.
i=1;
for f=1:length(x)
s(i)=x(i)+x(i+1);
i=i+1;
end
0 件のコメント
採用された回答
Geoff Hayes
2019 年 3 月 5 日
Karlie - f iterates from 1 to the length of your x array. This is fine except for
s(i)=x(i)+x(i+1);
because when i (and you could use f here) is the length of your array, then i+1 is one larger than the length and so x(i+1) is invalid giving you the above error message. Instead, try something like
for k=1:length(x)-1
s(k)=x(k)+x(k+1);
end
where we limit k to the interval [1, N-1] where N is the length your x array.
その他の回答 (1 件)
madhan ravi
2019 年 3 月 5 日
s=zeros(size(x)); % pre-allocate
for i = 1:numel(x) % assuming x a vector
% remove i=i+1
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!