寻找一种更有效的方法来编写我的MATLAB代码

寻找一种更有效的方法来编写我的MATLAB代码,matlab,Matlab,我已经用MATLAB写了以下内容 for i = 1:3 alpha11(i) = b+a.*randn(1,1); alpha22(i) = b+a.*randn(1,1); alpha12(i) = b+a.*randn(1,1); alpha21(i) = b+a.*randn(1,1); AoD11(i) = randi([-180/6 +180/6],1,1); AoA11(i) = randi([-180/6 +180/6 ],1,1); AoD22(i)

我已经用MATLAB写了以下内容

for i = 1:3

   alpha11(i) = b+a.*randn(1,1);
   alpha22(i) = b+a.*randn(1,1);
   alpha12(i) = b+a.*randn(1,1);
   alpha21(i) = b+a.*randn(1,1);

AoD11(i) = randi([-180/6 +180/6],1,1);
AoA11(i) = randi([-180/6 +180/6 ],1,1);
AoD22(i) = randi([-180/6 +180/6],1,1);
AoA22(i) = randi([-180/6 +180/6 ],1,1);


AoD21(i) = randi([-180 +180],1,1);
AoA21(i) = randi([-180 +180 ],1,1);
AoD12(i) = randi([-180 +180],1,1);
AoA12(i) = randi([-180 +180 ],1,1);


 ctet11(i)= ((2*pi)/lambda)*d*sin(AoD11(i));
 ctet22(i)= ((2*pi)/lambda)*d*sin(AoD22(i));
 ctet12(i)= ((2*pi)/lambda)*d*sin(AoD12(i));
 ctet21(i)= ((2*pi)/lambda)*d*sin(AoD21(i));

 f_t11_ula{i}=transpose((1/sqrt(M))*[ 1 exp(j*ctet11(i)) exp(j*2*ctet11(i)) exp(j*3*ctet11(i))  ]);
 f_t22_ula{i}=transpose((1/sqrt(M))*[ 1 exp(j*ctet22(i)) exp(j*2*ctet22(i)) exp(j*3*ctet22(i))  ]);
 f_t12_ula{i}=transpose((1/sqrt(M))*[ 1 exp(j*ctet12(i)) exp(j*2*ctet12(i)) exp(j*3*ctet12(i))  ]);
 f_t21_ula{i}=transpose((1/sqrt(M))*[ 1 exp(j*ctet21(i)) exp(j*2*ctet21(i)) exp(j*3*ctet21(i))  ]);


 cter11(i)= ((2*pi)/lambda)*d*sin(AoA11(i));
 cter22(i)= ((2*pi)/lambda)*d*sin(AoA22(i));
 cter12(i)= ((2*pi)/lambda)*d*sin(AoA12(i));
 cter21(i)= ((2*pi)/lambda)*d*sin(AoA21(i));

 f_r11_ula{i}=transpose((1/sqrt(O))*[ 1 exp(j*cter11(i)) exp(j*2*cter11(i)) exp(j*3*cter11(i))  ]); 
 f_r22_ula{i}=transpose((1/sqrt(O))*[ 1 exp(j*cter22(i)) exp(j*2*cter22(i)) exp(j*3*cter22(i))]); 
 f_r12_ula{i}=transpose((1/sqrt(O))*[ 1 exp(j*cter12(i)) exp(j*2*cter12(i)) exp(j*3*cter12(i))  ]); 
 f_r21_ula{i}=transpose((1/sqrt(O))*[ 1 exp(j*cter21(i)) exp(j*2*cter21(i)) exp(j*3*cter21(i))]); 



 channel11{i}=  alpha11(i) * f_r11_ula{i}* conj(transpose(f_t11_ula{i})) ;
 channel22{i}= alpha22(i) * f_r22_ula{i}* conj(transpose(f_t22_ula{i})) ;
 channel12{i}= alpha12(i) * f_r12_ula{i}* conj(transpose(f_t12_ula{i})) ;
 channel21{i}= alpha21(i) * f_r21_ula{i}* conj(transpose(f_t21_ula{i})) ;

end 
我在这里写这个问题是想问我如何压缩这个代码,因为你可以看到它写得不是很好,我基本上有很多重复。我不知道如何用几个命令来写它们。每个命令重复四次,并按11、12、21、22索引

如果有人想运行代码,需要以下变量

a = 1;
b = 0; 
M=4;
O = 4; 
lambda=0.15;
d=lambda/2;

期待您的建议

正如@David提到的,可以使用3D阵列来完成。这将消除大部分代码重复

下面是一个如何做到这一点的示例:

sz=[2,2,3];
alpha=b+a.*randn(sz);
AoD = randi([-180/6 +180/6],sz);
AoA = randi([-180 +180],sz);
mask = logical(repmat(eye(2),1,1,3));
[AoA(mask), AoD(~mask)] = deal(AoD(~mask),AoA(mask));
ctet = 2*pi/lambda * d * sin(AoD);
f_t = reshape(arrayfun(@(x) exp(1j*(0:3)'*ctet(x))/sqrt(M),1:12,'UniformOutput',0),sz);
cter = (2*pi)/lambda*d*sin(AoA);
f_r = reshape(arrayfun(@(x) exp(1j*(0:3)'*cter(x))/sqrt(O),1:12,'UniformOutput',0),sz);
channel = reshape(arrayfun(@(x) alpha(x) * f_r{x} * conj(transpose(f_t{x})), 1:12, 'UniformOutput',0),sz);
请注意,对于问题中提到的每个变量,可以使用相应的3D索引访问相同的值。例如,使用上面的代码,以前在
AoA11(1)
中的值现在在AoA(1,1,1)中。类似地,存储在
channel11{1}
中的矩阵现在位于
channel{1,1,1}


希望这能有所帮助。

您可能会用
变量(:,:,i)
替换每批
变量11(i)
变量12(i)
变量21(i)
来处理3D阵列。然后可以用
alpha(:,:,i)=b+a*randn(2,2)
替换前4行。事实上,您可能能够避免
for
循环,您可以使用
alpha=b+a*randn(2,2,3)
生成最终的
alpha
(实际上,对于您正在执行的某些操作来说,它可能没有那么简单)。非常感谢。您是否建议我上面的每个变量都可以替换为变量(:,:,I)?这和for循环中的i相同吗?那么我该如何区分这四种不同的情况呢?
variable1(I)
将对应于
variable(1,1,I)
variable21(I)
variable(2,1,I)
,等等。我想你应该能够这样做所有操作,这可能有点棘手,但是,如果遇到问题,您可以随时编辑问题。谢谢,在for循环中使用变量之前,我是否必须定义变量的维度。我试图简单地在for循环中编写alpha(:,:,I)=b+a rand(1,1)。我得到的误差指数超过了矩阵维数,如果我试着计算alpha(2,1,3)Do
rand(2,2)
。执行repmat时,我收到一个错误,错误如下:输入参数太多..你能告诉我更多细节吗?这在我的matlab版本中有效。如果它不适合您,请尝试使用采用维度向量的形式:mask=logical(repmat(eye(2),[1,1,3]);