필터 지우기
필터 지우기

Matlab: store input variables for arrayfun

조회 수: 1 (최근 30일)
yoni verhaegen
yoni verhaegen 2018년 6월 4일
댓글: Star Strider 2018년 6월 4일
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!

채택된 답변

Star Strider
Star Strider 2018년 6월 4일
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
yoni verhaegen
yoni verhaegen 2018년 6월 4일
Thanks! What should I change to this code if I want to include a range of values for 'fs' = linspace(0,50,20) as well?
Star Strider
Star Strider 2018년 6월 4일
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.)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by