skbn
skbn

Reputation: 33

How can I define a unknown value in a function?

I have a m.file as,

for ii=1:40
    m=round(X(ii,:)); % X is a matrix (40*1)
.
.
end

also, I have a function as,

function cost=MY_Fun(X)

 IW_Num=m*7;LW_Num=1*m;b1_Num=m*1;b2_Num=1*1;
.
.
end

Because value of m in function is not known, I get an error. How can I define value of m in function so that each value of m from "for..end" loop be defined in function??

Upvotes: 1

Views: 458

Answers (2)

Cape Code
Cape Code

Reputation: 3574

You can put m as an argument in your function:

function cost=MY_Fun(X,m)

 IW_Num=m*7;LW_Num=1*m;b1_Num=m*1;b2_Num=1*1;
.
.
end

Note that Matlab is good at applying its functions to arrays, so your first loop might be unnecessary, you could use:

m=round(X);

And then call your function with m(ii);

Upvotes: 1

AdityaPande
AdityaPande

Reputation: 86

Try declaring the variable m global

http://www.mathworks.in/help/matlab/ref/global.html

OR

You can pass the value of m to the function as a parameter

Upvotes: 2

Related Questions