【问题标题】:ArrayFun with multiple input具有多个输入的 ArrayFun
【发布时间】:2014-03-06 12:41:36
【问题描述】:

我在 Matlab 中使用 Arrayfun 时遇到问题。我有一个角度向量,我想避免 for loop 并申请:

rmatrix1 = arrayfun(...
    @(x) anglesLoop(iblade, iradius, jradius, ifrequency, x, rot), ...
    angles, 'uniformoutput', false);

其中iblade = 1iradius = 1jradius = 1ifrequency = 1rot = 0.5

函数如下:

%% Angles Loop
function rmatrix = anglesLoop(iblade, iradius, jradius, ifrequency, angles, rot)
global frequency blade
fshifted =(frequency(ifrequency)-angles*rot);
S11 = kaimal(fshifted);
S22 = S11;                 
r = distance(iradius,jradius,angles);
aux = (3/(2*pi)).*coherence(r).*exp(-1i*angles*ifrequency*blade(iblade)/3);                
rmatrix = (sqrt(S11).*sqrt(S22).*aux.*exp(1i*2*pi.*angles.*1/3));
end

带有子功能

%% distance for coherence function
function r=distance(x1,x2,theta)

r = sqrt(x1^2+x2^2- 2*x1*x2*cos(theta));
end

还有

%% Coherence
function gamma=coherence(r)
global frequency v10 L1
if r==0
   gamma=1;
else
   gamma=exp(-12.*sqrt((frequency.*r./v10).^2+(0.12.*r./L1).^2));
end

问题是当我在arrayfun 中应用anglesLoop 函数时,我获得了一个包含64 个不同数组的单元格,而我应该获得一个64 的向量,即角度长度。 rmatrix1 应该是 64 个元素的向量。有人可以给我一些建议吗?

【问题讨论】:

    标签: matlab


    【解决方案1】:

    我认为问题在于您要求'UniformOutput', false 导致返回为cell 数组。然后,您只需连接元胞数组的内容,您可以使用cell2mat 来完成。这是一个更简单的例子。我有一个行向量,我正在应用一个将每个元素转换为 2x2 矩阵的函数。我想最终将所有这些小矩阵连接在一起。

    rowVector = 1:5;
    myFcn = @(x) [x, -x; -x, x];
    separateMatricesCell = arrayfun(myFcn, rowVector, 'UniformOutput', false);
    concatenatedMatrices = cell2mat(separateMatricesCell)
    

    【讨论】:

      猜你喜欢
      • 2014-04-17
      • 1970-01-01
      • 1970-01-01
      • 2013-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-29
      • 2021-12-17
      相关资源
      最近更新 更多