index out of bounds because numel(averg)=1

2 ビュー (過去 30 日間)
Benjamin
Benjamin 2012 年 8 月 8 日
This is the code that I'm currently using:
A = interpolate;
averg = mean([A(1:end-2),A(3:end)],2);
streaking = [];
for idx = 2:size(A,2)-1
streaking(:,idx) = (abs(A(:,idx)-averg(idx))./averg(idx))*100;
end
The full error message:
>> Attempted to access averg(2); index out of bounds because numel(averg)=1.
>> Error in rad_cal2 (line 817)
streaking(:,idx) = (abs(A(:,idx)-averg(idx))./averg(idx))*100;
interpolate is a matrix

採用された回答

Laura Proctor
Laura Proctor 2012 年 8 月 8 日
編集済み: Laura Proctor 2012 年 8 月 8 日
You are using linear indexing to index into A, and so the output is a row vector. For example: A(1:end-2) will take all terms except for the last two in the matrix A, and create a row vector. Concatenating A(1:end-2) with A(3:end) creates a really long row vector. The mean of a row vector along the row, takes the average of all the elements which produces a scalar value for averg.
If you want to take the ROWS 1:end-2, then you can index into A using row column indexing:
A(1:end-2,:)
For what values are you trying to find the means? How are you trying to arrange the values in A to find the means? Be careful how you find that matrix ([A(1:end-2),A(3:end)]) - you may want to add an intermediate variable to calculate it, and then find the mean.
A = interpolate;
B = (A(1:end-2,:) + A(3:end,:))/2;
averg = mean(B,2)
modified accordingly.
  7 件のコメント
Benjamin
Benjamin 2012 年 8 月 8 日
Is there any benefit to using your input Sean opposed to Laura's method? If there is how would you adapt it to this:
averg = (A(1:end-2,:) + A(3:end,:))/2;
Sean de Wolski
Sean de Wolski 2012 年 8 月 8 日
No, just a different approach. More adaptable if say you wanted to use a longer window length. E.g. to change mine to include 5 elements one each side:
averg2 = conv2(A,ones(11,1)./11,'valid');
To adapt it to your above:
averg2 = conv2(A,[0.5;0;0.5],'valid')

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by