开发者

Matlab Matrix simple work

I have a very simple problem.

Suppose we have the following code to calculate directivity of isotropic antenna.

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

This is correct code which gives a value very close to 1 which should be for an isotropic antenna. Its just a sanity check so that when we have the actual matrix of 72*37, we can confirm that our code is correct.

Now the problem is that in the above example, we took a 72*37 matrix and did our integral approximation and got ONE value of directivity.

What开发者_Python百科 I need is to calculate directivity at every cell value of the 72*37 matrix. So the result would be another 72*37 matrix showing the calculated value of directivity at each cell value( which in this ideal case is 1). So for this example, currently we are getting the result as only one value of directivity. We need this value at every cell of the U_iso matrix. This would result in a 72*37 matrix with same value in it. Moreover, all values in the matrix would be same as the result from the above code.

So can you help me in this. I cant understand how to move the loop accross the matrix. So it calculates for each cell.

Awaiting reply.


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));


rwongs answer is the best in vectorising what you are after. otherwise, to answer your question about moving the loop across. you can do this:

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

is you do sum(D_iso(:)) that will sum all the elements and you should get what you had before.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜