2014-03-06 47 views
0

在Matlab中使用Arrayfun時遇到問題。我有角度的矢量和我想避免一個for loop我申請:帶有多輸入的ArrayFun

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位向量h是角度長度。 rmatrix1應該是64個元素的向量。有人可以給我一些建議嗎?

回答

0

我認爲問題是你要求的'UniformOutput', false導致返回爲cell陣列。然後你只需要連接單元格數組的內容,你可以用cell2mat來完成。這是一個更簡單的例子。我有一個行向量,我正在應用一個函數,將每個元素轉換爲一個2x2矩陣。我想結束所有這些小矩陣連接在一起。

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