Index exceeds the number of array elements (1264).

1 回表示 (過去 30 日間)
Karlie Brillant
Karlie Brillant 2019 年 3 月 5 日
コメント済み: Karlie Brillant 2019 年 3 月 5 日
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

採用された回答

Geoff Hayes
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 件のコメント
Karlie Brillant
Karlie Brillant 2019 年 3 月 5 日
This worked perfectly!

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

その他の回答 (1 件)

madhan ravi
madhan ravi 2019 年 3 月 5 日
s=zeros(size(x)); % pre-allocate
for i = 1:numel(x) % assuming x a vector
% remove i=i+1

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by