# Matlab trying to access index zero of a matrix, even though the boundaries are specified!

조회 수: 2 (최근 30일)
Philip 2014년 10월 15일
댓글: Philip 2014년 10월 15일
Hi! I have this Matlab problem that's driving me crazy! I'm trying to get values from a matrix and use these in other functions, but Matlab is giving me errors that doesn't make any sense. Here's the code: (R,W,T and v0 are constants declared in the beginning of the code. [t,x] returns t as a 81x1 matrix, and x as a 81x4 matrix).
[t,x]=ode45(@func2,[0 T],[0;v0;0;0]);
r=@(i)R-x(i,1);
fi=@(i)W*t(i)+(x(i,2));
er=@(alpha)[cos(alpha);sin(alpha)];
for i=1:length(t)
RV(i,:)=R*er(W*t(i));
RD(i,:)=r(t(i))*er(fi(i));
end
for i=1:length(t)
RB(i)=abs(RV(i,:)-RD(i,:));
end
plot(RB)
Here's the problem: When I run this i get the error:
"Attempted to access x(0,1); index must be a positive integer or logical.
Error in @(i)R-x(i,1)"
Even though it is specified in the for-loops that the value of i should start with 1. Declaring i as a variable with the value 1 doesn't help, neither does changing the name from i to something else in the functions before the for-loops. I don't understand why it is trying to access index 0!
Would greatly appreciate any help i can get!

댓글을 달려면 로그인하십시오.

### 채택된 답변

Julia 2014년 10월 15일
Hi,
the error occurs for the second line:
r=@(i)R-x(i,1);
You use i as the argument of that function. So i can have the value 0.
##### 댓글 수: 2없음 표시없음 숨기기
Iain 2014년 10월 15일
r(t(i)) - Thats probably what's causing the problem. - t(1) ought to be 0...
Philip 2014년 10월 15일
Oh! I get it! Thank you so much!

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by