非标量在arrayfun统一输出错误。怎么修? [英] Non-scalar in Uniform output error in arrayfun. How to fix?

查看:316
本文介绍了非标量在arrayfun统一输出错误。怎么修?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这意味着什么在这种情况下。我尝试添加'uniformoutput',false以arrayfun的结束,但随后它与+操作​​符说生气了未定义操作符'+'类型'细胞'的输入参数。
我改成了+,但得到了解析错误:(我是什么做错了?

I'm not sure what it means in this context. I tried adding " 'uniformoutput',false " to the end of arrayfun, but then it got upset with the "+" operator saying "Undefined operator '+' for input arguments of type 'cell'." I changed it to ".+" but got a parse error :( What am doing wrong?

下面是破碎部和错误的图像。整个code低于万一有人想尝试运行它或复制破损的部分。

Here is an image of the part that is broken and the error. The entire code is below in case someone would like to try running it or copy the broken part.

整个code:

    function gbp2(zi,zf)

global beam xlist ylist wi qi Ri wf qf Rf Psii Psif x n
beam = struct('E',[],'lambda',[],'w',[],'R',[],'q',[],'a',[]);

E = 1;                              % electric field
lambda = 1064*10^-9;                % wavelength
k = 2*pi/lambda;                    % wave number
wi = 10^-3;                         % initial waist width (minimum spot size)
zr = (pi*wi^2)/lambda;              % Rayleigh range
Ri = zi + zr^2/zi;
qi = 1/(1/Ri-1i*lambda/(pi*wi^2));  % initial complex beam parameter
Psii = atan(real(qi)/imag(qi));     % Gouy phase
mat = [1 zf; 0 1];                  % transformation matrix
A = mat(1,1); B = mat(1,2); C = mat(2,1); D = mat(2,2);
qf = (A*qi + B)/(C*qi + D);
wf = sqrt(-lambda/pi*(1/imag(1/qf)));
Rf = 1/real(1/qf);

u = @(z, coor, mode, w, R, Psi) (2/pi)^(1/4)*sqrt(exp(1i*(2*mode+1)*Psi)/(2^mode*factorial(mode)*w))*...
            hermiteH(mode,sqrt(2)*coor/w)*exp(-coor^2*(1/w^2+1i*k/(2*R))-1i*k*z);

% -------------------- ERROR IN THIS PIECE (below) ----------------------------
xlist = containers.Map('KeyType','double','ValueType','any');
ylist = containers.Map('KeyType','double','ValueType','any');

    function pts(z, w, R, Psi)
        xlist(z) = -2*w:10^-4:2*w;
        ylist(z) = zeros(1,size(xlist(z),2));
        for mode = 0:2:10
            ylist(z) = ylist(z) + arrayfun(@(coor) u(z, coor, mode, w, R, Psi),xlist(z),'uniformoutput',false);
        end
    end

pts(zi,wi,Ri,Psii)
pts(zf,wf,Rf,Psif)
plot(xlist(zi),ylist(zi),xlist(zf),ylist(zf)) end

我试着写一个类似但更简单的功能,它似乎只是罚款工作:

I tried writing a similar but simpler function and it seems to work just fine:

function test(zi, zf)

u = @(z,coor,mode) z*mode + integral(@(coor)coor,0,1);
xlist = containers.Map('KeyType','double','ValueType','any');
ylist = containers.Map('KeyType','double','ValueType','any');

    function pts(z)
        xlist(z) = -5:5;
        ylist(z) = zeros(1,size(xlist(z),2));
        for mode = 0:2:10
            ylist(z) = ylist(z) + arrayfun(@(coor) u(z,coor,mode),xlist(z));
        end
    end

pts(zi)
pts(zf)
plot(xlist(zi),ylist(zi),xlist(zf),ylist(zf))

end

所以我不知道这个问题是我的code的内容。

so I'm not sure what the problem is with my code.

推荐答案

错误信息给你一个很大的提示去哪里找:

The error message give you a big hint where to look:

未定义操作符'+'类型'细胞'的输入参数。

Undefined operator '+' for input arguments of type 'cell'.

错误的 GBP2 / PTS (第36行)结果
                     ylist(z) = ylist(Z)+ arrayfun(@(COOR)U(Z,COOR,模式,W,R,结果
                     Psi),xlist(z).','uniformoutput',false);

Error in gbp2/pts (line 36)
                   ylist(z) = ylist(z) + arrayfun(@(coor) u(z, coor, mode, w, R,
                   Psi),xlist(z).','uniformoutput',false);

arrayfun 'UniformOutput 选项:

请求将 arrayfun 功能结合到输出单元阵列中的 B1,...,家蚕。功能输出的 FUNC 可以是任何尺寸或类型。

Requests that the arrayfun function combine the outputs into cell arrays B1,...,Bm. The outputs of function func can be of any size or type.

事实上,如果你检查,这条线的输出是一个单元阵列:

Indeed, if you check, the output of this line is a cell array:

arrayfun(@(coor) u(z, coor, mode, w, R, Psi),xlist(z),'uniformoutput',false)

您不能直接从单元阵列的值相加。这里有几种方法可以做到这一个:

You can't sum the values from the cell array directly. Here's one of several ways you can do this:

v = arrayfun(@(coor) u(z, coor, mode, w, R, Psi),xlist(z),'uniformoutput',false);
ylist(z) = ylist(z) + [v{:}];

不过,我不明白为什么你需要使用'UniformOutput 选项,甚至慢 arrayfun 在所有。只是矢量化方面的功能, COOR

However, I don't see why you need to used the 'UniformOutput' option or even the slow arrayfun at all. Just vectorize your function with respect to coor:

u = @(z, coor, mode, w, R, Psi)(2/pi)^(1/4)*sqrt(exp(1i*(2*mode+1)*Psi)/(2^mode*factorial(mode)*w))*...
        hermiteH(mode,sqrt(2)*coor/w).*exp(-coor.^2*(1/w^2+1i*k/(2*R))-1i*k*z);

现在

ylist(z) = ylist(z) + u(z, xlist(z), mode, w, R, Psi);

结果
一些额外的建议:<一href=\"http://www.mathworks.com/matlabcentral/answers/51946-systematic-do-not-use-global-don-t-use-eval\"相对=nofollow>不要使用 全球变量 - 他们的低效并有几乎总是更好的解决方案。如果,就是要一个嵌套函数,你错过了一个闭合的结束为主体 GBP2 功能。这可能是重命名变量模式,以便它不超载的同名的内置函数是个好主意。 PSIF 没有定义。和零(1,大小(的Xlist(Z),2))可以简单地写成零(大小(的Xlist(Z)))


Some additional suggestions: Don't use global variables – they're inefficient and there are almost always better solutions. If pts is meant to be a nested function, you're missing a closing end for the main gbp2 function. It might be a good idea to rename your variable mode so that it doesn't overload the built-in function of the same name. Psif isn't defined. And zeros(1,size(xlist(z),2)) can be written simply as zeros(size(xlist(z))).

这篇关于非标量在arrayfun统一输出错误。怎么修?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆