Why do i have the "Array indices must be positive integers or logical values" error?
1 回表示 (過去 30 日間)
古いコメントを表示
Diego Ernesto Sandoval Vargas
2021 年 10 月 22 日
コメント済み: Diego Ernesto Sandoval Vargas
2021 年 10 月 22 日
Hi, im new in matlab and im trying to move one place to the right the elements of an array
cadena2 = [1,2,3,4,5];
n = length(cadena2);
aux = cadena2(n);
for i = n:-1:1
cadena2(i)=cadena2((i-1));
end
cadena2(1)=aux;
cadena2
i have this error:
Array indices must be positive integers or logical values.
Error in Clase01102 (line 22)
cadena2(i)=cadena2((i-1));
if you can help i really apreciate it
0 件のコメント
採用された回答
Jon
2021 年 10 月 22 日
編集済み: Jon
2021 年 10 月 22 日
Your problem is that in your loop i goes from 5 down to 1 but you index cadena2((i-1), and for the last loop when i = 1 this equals zero which is not allowed. Indices must be positive integers
The MATLAB function circshift is very helpful for this kind of operation. You can do it in one line
y = circshift(cadena2,1)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!