I want to define a vector function in x
like this [sin(x),sin(2*x), ... ,sin(100*x)]
where x
is a variable. What I defined is that :
for i=1:100H(i)=@(x) sin(i*x);end
But it doesn't work.
Best Answer
Method 1, use vectorization (simplest, fastest):
H=@(N,x) sin((1:N)'.*x);%With the '.*' operation u can create the full matrix in single call:N=100; x=0:0.01:pi;M=H(N, x)
Method 2, use arrayfun (can generalized to other functions):
N=10;H=@(x) arrayfun(@(i)sin(i*x),1:N);
You can even combine multiple functions in a function handle, e.g.
H = @(x) sin((1:length(x))'.*x);
This always returns a square matrix; in your case a 315x315 matrix. The first 100 rows are identical to the solution suggested by Mendi Barel