arraysfunctionmatlabindexingfunction-handle

Create an External N by 1 Function Handle Populated with Alternating Values Using an Internal Function Handle


I have the following internal function handle:

f = @(t)f_0.*sin(alpha.*t).*cos(beta.*t);

I want to create an external N by 1 function handle called F that places f in alternating positions in F as follows:

F = @t[f(t); 0; f(t); 0; f(t); 0; … ]

I would like to do this with the following function:

function F = farray (f,N)
    r = 2:2:N
    F = @(t) [f(t); zeros(N-r, 1)];
end

This doesn’t produce the desired population method. Can this be done without a for loop or another MATLAB function like toeplitz()?


Solution

  • Create a N by 1 vector that contains 0 and 1 in alternating positions then multiply it by f(t):

    function F = farray (f,N)
        z = zeros(N, 1);
        z(1:2:end) = 1;
        F = @(t) f(t) .* z;
    end
    

    Another way to create the array:

    function out = place_ft_at_alternating_positions(ft, N)
        out = zeros(N, 1);
        out(1:2:end) = ft;      
    end
    
    function F = farray (f, N)  
        F = @(t) place_ft_at_alternating_positions(f(t), N);
    end