Map function in MATLAB?

前端 未结 7 844
情书的邮戳
情书的邮戳 2020-11-29 17:11

I\'m a little surprised that MATLAB doesn\'t have a Map function, so I hacked one together myself since it\'s something I can\'t live without. Is there a better version out

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 18:10

    It seems that the built-in arrayfun doesn't work if the result needed is an array of function: eg: map(@(x)[x x^2 x^3],1:10)

    slight mods below make this work better:

    function results = map(f,list)
    % why doesn't MATLAB have a Map function?
    for k = 1:length(list)
        if (k==1)
            r1=f(list(k));
            results = zeros(length(r1),length(list));
            results(:,k)=r1;
        else
            results(:,k) = f(list(k));
    
        end;
    end;
    end
    

提交回复
热议问题