필터 지우기
필터 지우기

subscript indices must either be real positive integers or logicals when using a function in a nested for?

조회 수: 1 (최근 30일)
I use this function in a nested for and I get this error: subscript indices must either be real positive integers or logicals
Zpred = h(Xpred)
where Xpred is a matrix
function zk=h(x)
zk(1,:)=sqrt(900+(x(1,:)-30).^2);
zk(2,:)=x(2,:).*(x(1,:)-30)./zk(1,:)
can anyone help me to find the error

채택된 답변

Image Analyst
Image Analyst 2015년 5월 28일
When you're calling this:
Zpred = h(Xpred)
at that point, it thinks h is an array, not a function like you thought.
Your script/function is not called h.m is it? Or else somethere else earlier in your code, you must have used h in a way that created an array called h.

추가 답변 (1개)

Joseph Cheng
Joseph Cheng 2015년 5월 28일
편집: Joseph Cheng 2015년 5월 28일
so when i run the code you have:
function zk=h(x)
zk(1,:)=sqrt(900+(x(1,:)-30).^2);
zk(2,:)=x(2,:).*(x(1,:)-30)./zk(1,:);
with the input
h(randi(10,2,1))
i don't get the error. Now with this type of error the fastest thing is for you to use the debugger and solve it. Put a breakpoint at the call, verify the input Xpred is actually what you want, there is no variable or other item already called h, and if everything checks out step inside the function where is the line that the error says it is?
  댓글 수: 3
Joseph Cheng
Joseph Cheng 2015년 5월 28일
편집: Joseph Cheng 2015년 5월 28일
you should not use 'h' to describe a function and a variable. Matlab is not smart enough to understand which one you're trying to use.
Here is an example: if my dog is called Brian and I have a friend called Brian, and I ask you to take Brian outside. You wouldn't not know which one to take out side would you? Call your function another name or use another variable name for h.
*sorry to anyone named Brian. It was the first name i could think of.
sH
sH 2015년 5월 28일
편집: sH 2015년 5월 28일
no I meant that I'm calling function h for the second time. when I use it separately, I don't get any error but when I use it in a nested for in a larger simulation I get this error

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by