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

59 Views Asked by At

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()?

2

There are 2 best solutions below

1
rahnema1 On BEST ANSWER

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 
0
Wolfie On

You can create F using this, assuming N is even

F = @(t) repmat( [f(t); 0], N/2, 1 );