필터 지우기
필터 지우기

how can you pass values to variables in an an array of functions?

조회 수: 3 (최근 30일)
Leonard
Leonard 2014년 3월 22일
댓글: David Young 2014년 3월 24일
I am trying to make a movie of the solutions to the heat equation for various values of a parameter in the coefficients . I want to create an array for the fourier coefficients of the solution. How can I define an 'array function' ? does this idea exist.
apologies if theis is a duplicate question. couldn't find this answer anywhere.
  댓글 수: 6
Leonard
Leonard 2014년 3월 22일
what if the function which defined each cell was more complicated than an exponent?
so basically passing a value to a parameter (variable) in multiple cells in an array is something everyone should avoid?
Jan
Jan 2014년 3월 22일
@Leonard: If you have a more complicated function, you create a function file, which can be as complicated as you want.

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

답변 (1개)

per isakson
per isakson 2014년 3월 22일
편집: per isakson 2014년 3월 22일
Playing with anonymous functions in combination with cellfun
fh{1}=@(x) x.*x;
fh{2}=@(x) x.*x.*x;
fh{3}=@(x) x.*x.*x.*x;
fh{4}=@(x) sin(x);
fh{5}=@(x) cos(x);
foo = @(z) cellfun( @(f,x) f(x), fh, repmat({z},size(fh)), 'uni', false);
foo(2)
returns
ans =
[4] [8] [16] [0.9093] [-0.4161]
However, those are cell arrays. Matlab doesn't support ordinary arrays of anonymous functions.
.
Or a little better
foo = @(z) cellfun( @(f,x) f(x), fh, repmat({z},size(fh)), 'uni', true);
foo(2)
returns
ans =
4.0000 8.0000 16.0000 0.9093 -0.4161
which is an array of doubles.
.
I guess that what you want is a Functional programming languages
  댓글 수: 3
per isakson
per isakson 2014년 3월 23일
편집: per isakson 2014년 3월 23일
An array with ten identical functions might not be useful.
However, anonymous functions are powerful and it takes some experiments to learn how to use them. "[...] any votes on that idea?" I vote for keeping the post. I contributed an experiment.
David Young
David Young 2014년 3월 24일
Yes, building the 10 anonymous functions is being overcomplicated. Almost certainly you can do what you want just by passing vector arguments to ordinary functions. I'd say keep the post - there are many less interesting and less useful threads here.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by