2010-07-03 101 views
0

我有个很简单的问题。Matlab矩阵简单工作

假设我们有以下代码来计算各向同性天线的方向性。

ThDeg = 0:5:180; 
dtheta = 5*pi/180; 
dphi = 5*pi/180; 
Th = ThDeg*pi/180; 

% the above are the angles at which the following matrix is acquired in practical case. In this case we take a matrix of all ones. 

U_iso = ones(72, 37); % our matrix assumed 

omega_iso = 0; 
for i = 1:72 
    for j=1:37 
     omega_iso = omega_iso + U_iso(i,j)*sin(Th(j))*dphi*dtheta; 
    end 
end 

D_iso = 4*pi/omega_iso 

这是正确的代码,它给出了非常接近1的值,这应该是一个全向天线。它只是一个健全的检查,所以当我们有72 * 37的实际矩阵时,我们可以确认我们的代码是正确的。

现在的问题是,在上面的例子中,我们采用了72 * 37的矩阵,并做了我们的积分近似并得到了一个方向性值。

我需要的是计算72 * 37矩阵的每个单元值的方向性。因此,结果将是另一个72 * 37矩阵,显示每个像元值(在理想情况下为1)的计算值。因此,对于这个例子,目前我们只将结果作为方向性的一个值。我们在U_iso矩阵的每个单元格都需要这个值。这会导致72 * 37的矩阵具有相同的值。而且,矩阵中的所有值都与上面代码的结果相同。

所以你能帮助我吗?我无法理解如何移动矩阵中的循环。所以它计算每个单元格。

等待回复。

+0

你的意思是U_iso是矩阵的矩阵或什么? – kennytm 2010-07-03 19:30:01

+0

没有它只是一个假设矩阵。通常,这是完美的天线。但是当我进行测量时,实际的天线在其72 * 37矩阵中将具有不同的数字。这实际上是辐射模式。为了更简单,使用上面的U_iso。 – adeel 2010-07-03 20:26:48

+0

您可以使用采用相同大小矩阵的“元素”运算符,并对每个矩阵的每个(i,j)元素进行某种操作(例如乘法)。 – rwong 2010-07-03 20:30:38

回答

0
SinThJ = zeros(72, 37); 

% For each of the 72 x 37 cell, compute sin(Th(j)) 

for j = 1:37 
    SinThJ(:, j) = repmat(sin(Th(j)), 72, 1); 
end 

% Elementwise multiplication 
% This omega_iso becomes a matrix 

omega_iso = U_iso .* SinThJ * dphi * dtheta; 

% This is the integration of the matrix 

omega_iso_sum = sum(sum(omega_iso)); 
+0

很好的答案。您还可以使用sum(omega_iso(:))快速求和所有元素,而不管数组的维数有多少 – TDevlin 2015-12-10 23:23:48

0

rwongs答案是最好的矢量化你在之后。 否则,回答你关于移动循环的问题。你可以这样做:

ThDeg = 0:5:180; 
dtheta = 5*pi/180; 
dphi = 5*pi/180; 
Th = ThDeg*pi/180; 

% the above are the angles at which the following matrix is acquired in practical case. In this case we take a matrix of all ones. 

U_iso = ones(72, 37); % our matrix assumed 

omega_iso = zeros(72,37; 
for i = 1:72 
    for j=1:37 
     omega_iso(i,j) = omega_iso(i,j) + U_iso(i,j)*sin(Th(j))*dphi*dtheta; 
    end 
end 

D_iso = 4.*pi./omega_iso 

是你做的总和(D_iso(:)),将总结所有的元素,你应该让你有过什么。