フィルターのクリア

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

2 ビュー (過去 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 Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by