Index exceeds array bound help
조회 수: 9 (최근 30일)
이전 댓글 표시
Hello, I'm trying to have decimals in the for loop, I've used this way to do it but it showed me Index exceeds array bound. Is there any other way to have decimals in for loop too?
In this, magnew is 900 values from 3.1 to 7.9.
zz=1:0.1:10;
for z=1:91
a(z)=length(find(magnew>=(zz(z)) & magnew<(zz(z+1))))/(length(magnew));
end
댓글 수: 0
채택된 답변
Walter Roberson
2018년 3월 25일
length(zz) is 91, so up to zz(91) exists.
In your for loop you go z=1:91 so z can become 91. You index zz(z+1) so you try to index up to z(91+1) = z(92) which does not exist.
By the way, there is a way to make your calculation more efficient
Consider your sub-expression length(find(LOGICAL)) . find() is going to return one index for each location where LOGICAL is true, and length() of that is going to return the number of such indices. So the subexpression is counting the number of places that LOGICAL is true. One way of counting the places that are true is nnz(), so your code can be replaced with
a(z) = nnz(magnew >= zz(z) & magnew < zz(z+1)) / length(magnew)
But you can go further to make it more compact. Since you are dividing by the number of items, you are effectively calculating the fraction of the entries that are true. But the fraction that is true is the same as the mean:
a(z) = mean(magnew >= zz(z) & magnew < zz(z+1));
You can also do better still. In the case where the entries in z are sorted, your loop can be replaced by
counts = histc(magnew, zz);
a = counts(1:end-1)./length(magnew);
The reason you take counts(1:end-1) is that for histc, the last entry that is returned would be the count of the values that are exactly equal to zz(end) which is something that your loop does not calculate. Your other entries are counting the number of entries that are >= a(i) and strictly less than a(i+1), which is exactly what histc() does except for the last entry.
Note: the new histcounts() is not an exact substitute for histc() in this regard. histcounts() has the property of testing a(i) <= x < a(i+1) except for the last entry, for which the test is a(end-1) <= x <= a(end) which is not what your loop calculates.
추가 답변 (3개)
David Fletcher
2018년 3월 25일
편집: David Fletcher
2018년 3월 25일
On the last iteration of the loop z=91; you try to index z+1 (92) of array zz (which does not exist). On the last iteration of the loop z=91; you try to index z+1 (92) of array zz (which does not exist). Yes, you can have decimals in the for loop, allowing you to get rid of the array zz. You can probably also vectorize the loop out as well
댓글 수: 2
David Fletcher
2018년 3월 26일
편집: David Fletcher
2018년 3월 26일
Yes, effectively the loop variable replaces the zz array (though it would need some alteration as each iteration would only carry one of the zz values - in your code you indexed the present zz value and also the next zz value). Saying that, Walter Robinson's vectorized solution below is a superior way of doing it, though given the number of elements in this particular case you probably won't notice much difference.
sai krishna pervala
2018년 3월 31일
Index exceeds array bounds.
Error in gauss2 (line 65) plot(1:5:GaussItr,plotGauss(1:5:GaussItr),'LineWidth',2)
댓글 수: 0
kajal daksh
2020년 5월 21일
Hello,i am using accelerometer sensor ,where i need the following formula for my code ,but i am getting this index error array bound at A(init) line 11
init=1;
interval=200;
sum=0;
while(init<interval)
x(init)=readVoltage(a,'A1');
x_g =( ( ( (x * 5)/1024) - 1.65 ) / 0.330 );
y(init)=readVoltage(a,'A2');
y_g = ( ( ( (y * 5)/1024) - 1.65 ) / 0.330 );
z(init)=readVoltage(a,'A3');
z_g = ( ( ( (z * 5)/1024) - 1.80 ) / 0.330 );
A(init)=sqrt((x_g(init+1)-x_g(init))^2+(y_g(init+1)-y_g(init))^2+(z_g(init+1)-z_g(init))^2);
sum=sum+A(init);
end
댓글 수: 3
kajal daksh
2020년 5월 22일
편집: kajal daksh
2020년 5월 22일
thankyou so much for your response,i have corrected accordingly. but still having error like "array indices must be positive value or logical value"?
clc
clear
close all
a=arduino();
clear a;
a=arduino('COM3','Uno');
x=zeros();
y=zeros();
z=zeros();
while(1)
init=2;
interval=60;
sum=0;
%Fs=25;
%Ts=1/Fs;
%t=0:Ts:1;
while(init<interval)
x(init)=( ( ( (readVoltage(a,'A1') * 5)/1024) - 1.65 ) / 0.330 );
y(init)=( ( ( (readVoltage(a,'A2') * 5)/1024) - 1.65 ) / 0.330 );
z(init)=( ( ( (readVoltage(a,'A3') * 5)/1024) - 1.80 ) / 0.330 );
subplot(3,2,1)
plot(x,'b');
title('x');
subplot(3,2,3)
plot(y,'r');
title('y')
subplot(3,2,5)
plot(z,'g');
title('z');
pause(0.01)
A(init)=sqrt((x(init)-x(init-1))^2+(y(init)-y(init-1))^2+(z(init)-z(init-1))^2);
sum=sum+A(init);
init=init+1;
end
end
참고 항목
카테고리
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!