Why did I receive this error with inline
3 ビュー (過去 30 日間)
古いコメントを表示
I have an inline object defined in the following manner (Part of code):
for o= 1 : 6
f = inline('min(x.+d(o,5),-x.+d(o,3))');
%%where d is a matix
.
.
for k= 1 : 3
.
.
val (k)=f(Pbest (k,1));
end
end
Error using inline/subsref (line 13)
Not enough inputs to inline function.
1 件のコメント
回答 (3 件)
the cyclist
2012 年 7 月 5 日
The way you have defined you inline function, it is going to contain the variable o, rather than replacing it with the loop's value of o.
I think what you want instead is this:
f = inline(['min(x.+d(',num2str(o),',5),-x.+d(',num2str(o),',3))'])
Also, o is a terrible name for a variable. ;-)
0 件のコメント
Honglei Chen
2012 年 7 月 5 日
編集済み: Honglei Chen
2012 年 7 月 5 日
I see several issues:
- You should use + instead of .+
- The error is due to the fact that there are three variable in your function: x,d, and o. You should define them. As is, MATLAB thinks that d is a function but then there should be two inputs and you invoked it with only one input.
Below is a working example:
f = inline('min(x+d(o,3),-x+d(o,3))','x','o','d')
f(1,2,magic(5))
0 件のコメント
Star Strider
2012 年 7 月 5 日
First, you only need to define your 'inline' function once, ideally near the beginning of your code. Put it before your loop.
Second, it needs to know what you want it to do, so you have to specify your arguments to it:
f = inline('min(x+d(o,5), -x+d(o,3))', 'x', 'd', 'o');
I have no idea what you want to do with your 'val(k) = ...' assignment, since it doesn't seem to match what you want 'f' to do.
Third, in my experience, it's not necessary to use the '.' operator with '+' and '-' since they operate element-wise anyway. It's only necessary to use it with '*', '/', and '^'.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Function Creation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!