Index exceeds array bound help

9 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 25 Mar 2018
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.
  1 Comment
Jeng Hann Chong
Jeng Hann Chong on 25 Mar 2018
Thank you for your explanation, Walter. I like the 3rd one of yours.

Sign in to comment.

More Answers (3)

David Fletcher
David Fletcher on 25 Mar 2018
Edited: David Fletcher on 25 Mar 2018
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 Comments
Jeng Hann Chong
Jeng Hann Chong on 25 Mar 2018
Thank you for your help David, do I put the decimals in the for loop like
for z=1:0.1:10
David Fletcher
David Fletcher on 26 Mar 2018
Edited: David Fletcher on 26 Mar 2018
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.

Sign in to comment.


sai krishna pervala
sai krishna pervala on 31 Mar 2018
Index exceeds array bounds.
Error in gauss2 (line 65) plot(1:5:GaussItr,plotGauss(1:5:GaussItr),'LineWidth',2)

kajal daksh
kajal daksh on 21 May 2020
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 Comments
kajal daksh
kajal daksh on 22 May 2020
Edited: kajal daksh on 22 May 2020
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
Walter Roberson
Walter Roberson on 22 May 2020
when init is 1, what is x(init-1) ?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!