0

I have the following function:

function F = farray (f,N)
F = {@(t) zeros(N,1)};
    for i=1:N
        if mod(i,2)==0
            F{i}=f(t);
        end
    end
F = cell2mat(F);
end

f is a function handle of time, that is f=@(t)f(t). N dictates the size of the output array F. Similar to f, F is also a function of time. I would like to populate every other entry of F with f, and return F as a function handle that converts to a double array when substituted for t. All other entries in the array should be zero. I thought what I have above would work, but that is not the case. Where am I going wrong here?

2
  • An array of functions is not the same as a function that returns an array. You want to write a function that evaluates y = f(t) and then returns an array that alternates 0 and y. Does this need to be an anonymous function or can you write a regular function? (Note that you can have a handle to any type of function!) Commented Apr 16, 2024 at 18:20
  • @CrisLuengo. I would like the latter, a function that returns an array. I am looking for an anonymous function that can be used for any value of t Commented Apr 16, 2024 at 18:33

1 Answer 1

1

I would do this this way: write a function that creates the desired output array for a given f, t and N, and then use an anonymous function to fill in the existing definitions of f and N so that you have a function only of t.

f = …
N = …
F = @(t) farray(f, t, N);

function out = farray (f, t, N)
    out = zeros(N,1);
    out(2:2:end) = f(t);
end
Sign up to request clarification or add additional context in comments.

1 Comment

This works quite well. Thank you. @CrisLuengo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.