Matlab: store input variables for arrayfun
1 view (last 30 days)
Show older comments
yoni verhaegen
on 4 Jun 2018
Commented: Star Strider
on 4 Jun 2018
Hi all,
I have the following function:
function [value_final] = myvalue(fd)
value_final = (fs + (fd.*(th.^2)));
end
Values for th and fs are constant, but for fd I use
fd = linspace(0,50,20);
th=5;
fs=8.5;
B = arrayfun(@myvalue,fd);
How can I create a matrix with in the first column each input value for 'fd' and in the second column the corresponding value for 'value_final' for each iteration? Later I want to include a range of values for 'fs' as well, so it would be nice to know which combination yields which value.
Thanks!
0 Comments
Accepted Answer
Star Strider
on 4 Jun 2018
First, rewrite the function to include the additional parameters:
function [value_final] = myvalue(fd,fs,th)
value_final = (fs + (fd.*(th.^2)));
end
then pass them to it using an anonymous function within arrayfun:
fd = linspace(0,50,20);
th=5;
fs=8.5;
B = arrayfun(@(fd)myvalue(fd,fs,th),fd(:));
Out = [fd(:) B]
Since ‘myvalue’ is an external function, not an anonymous function, you have to pass the additional parameters to it. It will not pick them up from your workspace.
See if this does what you want.
2 Comments
Star Strider
on 4 Jun 2018
As always, my pleasure!
This will do what you want:
fd = linspace(0,50,20);
th=5;
fs = linspace(0,50,20);
B = arrayfun(@(fd)myvalue(fd,fs,th),fd(:), 'UniformOutput',false);
Bmtx = cell2mat(B);
Out = [fd(:) Bmtx];
Note that for this to work as you want it to, ‘fs’ must remain a row vector (as it is currently defined in this code). The ‘(:)’ in ‘fd(:)’ forces it to be a column vector. (The arrayfun call will still work if you force ‘fs’ to also be a column vector. The concatenation to create the ‘Out’ matrix will not, since the row sizes will not be the same.)
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!