matrix - Take product of function handles within a cell and then evaluate for given values in MATLAB -
i have cell of function handles , take product of each row.
i want evaluate function handles within cell values.
example:
original cell:
p{1,1} = @(x) x p{1,2} = @(y) y p{2,1} = @(x) x p{2,2} = @(y) y.^2 desired product:
p{1,1} = @(x,y) x.*y p{2,1} = @(x,y) x.*y.`2 then evaluate
x = 2:0.1:3; y = 1:0.1:2; then suppose use cell2mat p?
i have been trying use cellfun, not sure if correct when using anonymous functions.
would appreciate advice!
you can reshape input vectors x , y 3d vectors , apply function on them:
p{1,1} = @(x) x ; p{1,2} = @(y) y; p{2,1} = @(x) x ; p{2,2} = @(y) y.^2; %reshape input 3d x=reshape(2:0.1:3,1,1,[]); y=reshape(1:0.1:2,1,1,[]); %empty cell of results c = cell(size(p)); %apply functions of each column related input c(:,1) = cellfun(@(f)f(x),p(:,1),'uniformoutput', false); c(:,2) = cellfun(@(f)f(y),p(:,2),'uniformoutput', false); %convert cell array = cell2mat(c); %take product of each row rowprod = prod(s,2) %remove singleton dimensions out = squeeze(rowprod)
Comments
Post a Comment