index out of bounds because numel(averg)=1

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일

0 개 추천

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일
What if i need to pull values from each column separately. So let's say i have 10 columns. Then i need to pull out the 1:end-2 and the 3:end values and average those together for each column?
It depends on what you are trying to average. It would help if you could explain in words what you are trying to do with this line:
averg = mean([A(1:end-2),A(3:end)],2);
Benjamin
Benjamin 2012년 8월 8일
편집: Benjamin 2012년 8월 8일
I'll just explain the entire process of what I'm trying to do. So Interpolate is a matrix that changes size from each file that I'm reading in. I want to take the value before A(1:end-2) and the value after A(3:end) each value per column (exluding the first and last value from the column) and find their average. and then apply it to this:
streaking(:,idx) = (abs(A(:,idx)-averg(idx))./averg(idx))*100;
Laura Proctor
Laura Proctor 2012년 8월 8일
편집: Laura Proctor 2012년 8월 8일
So, if I understand, you want an average of three values (you didn't explicitly say to include the middle value itself, so I'm making that assumption). Also, you are excluding the entire first row and entire last row from what I understand - it would look something like this:
averg = (A(1:end-2,:) + A(2:end-1,:) + A(3:end,:))/3;
averg2 = conv2(A,ones(3,1)./3,'valid');
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;
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개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by