How can I fix "Index in position 2 exceeds array bounds" in for loop ?
1 回表示 (過去 30 日間)
古いコメントを表示
Code:
A= datafile
sumArray = [];
for c = 1:length(A)
col = A(:,c);
sum1 = rms(A(:,c));
sumArray = [sumArray,sum1];
end
s=sumArray'
Attached file = A
Error : Index in position 2 exceeds array bounds (must not exceed 11).
0 件のコメント
採用された回答
CHIRANJIT DAS
2022 年 11 月 7 日
@Rajeev Kumar You are giving dimension of row in the for loop. Perhaps you can replace length(A) with size(A,2). Revised code looks like the below one..
sumArray = [];
for c = 1:size(A,2)
col = A(:,c);
sum1 = rms(A(:,c));
sumArray = [sumArray,sum1];
end
s=sumArray'
Cheers
その他の回答 (2 件)
Askic V
2022 年 11 月 7 日
編集済み: Askic V
2022 年 11 月 7 日
This is because you're not using length correctly. In fact it is not a good idea to use length when working with matrices.
In your special case, please have a look at the code:
load A.mat
% A is a matrix with dimensions: 1024x11
[nrRows, nrCols] = size(A)
ll = length(A)
I hope you understand now. You have only 11 columns in a matrix, but you're trying to iterate loop from 1 to 1024, where in fact 1024 is the number of rows.
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!