Index exceeds array bound help
Show older comments
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
More Answers (3)
David Fletcher
on 25 Mar 2018
Edited: David Fletcher
on 25 Mar 2018
1 vote
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
on 25 Mar 2018
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.
sai krishna pervala
on 31 Mar 2018
0 votes
Index exceeds array bounds.
Error in gauss2 (line 65) plot(1:5:GaussItr,plotGauss(1:5:GaussItr),'LineWidth',2)
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
Walter Roberson
on 21 May 2020
Unless you pre-initialize x, you are extending x by one sample each time when you assign to x(init). Then x_g would be calculated based upon all existing x, so it would be the same size as x, probably init items long. Then when you go to assign to A(init) you access x_g(init+1), but that probably does not exist.
When you are just starting out, you only have one value in x_g, y_g, z_g and it does not make sense to calculate based upon the difference in adjacent samples. After the first time, when init=2 or more, it only makes sense to calculate based upon the difference between the current one and the previous one, not between the current one and the next one (that you have not received yet.)
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
on 22 May 2020
when init is 1, what is x(init-1) ?
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!