简体   繁体   中英

How to apply a function to an array in MatLab with multiple arguments?

I have an array called contin array like [10,100,1000] , and I'd like to apply a function called transcontin to that array. I know that the way to do this is arrayfunc(@transcontin, contin) , if there is only one input to the function.

But, the function transcontin takes three inputs: Temp , b , and H . And, I need it to pass the values of the array into the function transcontin as b .

But, I have to loop through values of Temp and H . These values of Temp and H stay the same throughout each application to contin .

How do I accomplish this? How do I do arrayfunc(@transcontin, contin) but pass in the other two parameters that do not come in as an array?

Example input: contin=[10, 100, 1000]

Function:

function result=transcontin(Temp,b,h)
   result=b+Temp^h;
end

Let's say I want to fix Temp and h, and pass the elements of the array in as b :

Temp=5;
h=(1/3);

The output, after I do something like this upon the suggestion from @Edric arrayfun(@b transcontin(Temp, b, h), contin) , should be a vector transformed into:

[10+5^(1/3), 100+5^(1/3), 1000+5^(1/3)]

how to accomplish this?

You can "bind" in the constant arguments to an anonymous function handle, something like this (I think):

out = arrayfun(@(x) transcontin(Temp, x, H), contin)

The first argument to arrayfun is the anonymous function handle - that syntax creates a function that takes a single argument as required by arrayfun , and "binds" in the constant values Temp and H . More in the doc here: https://uk.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM