Matlab Matrix简单的工作

时间:2010-07-03 19:23:42

标签: 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矩阵具有相同的值。此外,矩阵中的所有值都与上述代码的结果相同。

所以你可以帮助我。我无法理解如何在矩阵中移动循环。所以它计算每个细胞。

等待回复。

2 个答案:

答案 0 :(得分: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));

答案 1 :(得分: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(:))将总结所有元素,你应该得到你以前的。