Index exceeds array bound help
이전 댓글 표시
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
채택된 답변
추가 답변 (3개)
David Fletcher
2018년 3월 25일
편집: David Fletcher
2018년 3월 25일
1 개 추천
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
Jeng Hann Chong
2018년 3월 25일
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일
0 개 추천
Index exceeds array bounds.
Error in gauss2 (line 65) plot(1:5:GaussItr,plotGauss(1:5:GaussItr),'LineWidth',2)
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
Walter Roberson
2020년 5월 21일
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
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
Walter Roberson
2020년 5월 22일
when init is 1, what is x(init-1) ?
카테고리
도움말 센터 및 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!