f



Problem in code...Need help urgent

Hello,
I wrote the same program twice.

following are the two codes.

The upper one (slower) performs correctly, while the lower one(faster) doesn't produce the same result!

Can anybody look into the program! I cant figure out what's the problem in the lower code?

Thanks!

Yi

%%%psi : 400x2x3350 deltax=deltay=1 ph_Tx ,ph_Rx : 1x3350 zeta:2x3350
%%%%%code 1
for ii = 1 : 400
    for ll = 1 : 3350
         gamma(ii,1,ll) = sinc(zeta(1,ll)*deltax*cos(ph_Tx(ll))/pi)*            sinc(zeta(1,ll)*deltay*sin(ph_Tx(ll))/pi)*psi(ii,1,ll);

         gamma(ii,2,ll) = sinc(zeta(2,ll)*deltax*cos(ph_Rx(ll))/pi)* sinc(zeta(2,ll)*deltay*sin(ph_Rx(ll))/pi)*psi(ii,2,ll);

    end
end

%%%%%%%code 2
gamma(1,:,:) = squeeze(psi(:,1,:)).* repmat(sinc(zeta(1,:).*cos(ph_Tx)*deltax/pi),size(psi,1),1).* repmat(sinc(zeta(1,:).*sin(ph_Tx)*deltay/pi),size(psi,1),1);

gamma(2,:,:) = squeeze(psi(:,2,:)).* repmat(sinc(zeta(2,:).*cos(ph_Rx)*deltax/pi),size(psi,1),1).* repmat(sinc(zeta(2,:).*sin(ph_Rx)*deltay/pi),size(psi,1),1);
0
chenyilll (1)
3/26/2009 12:59:01 AM
comp.soft-sys.matlab 211266 articles. 23 followers. lunamoonmoon (257) is leader. Post Follow

1 Replies
742 Views

Similar Articles

[PageSpeed] 54

Hi Yi,
Your first piece of code looks quite inefficient to me. You should exchange the order of the loops so that the lowest dimension updates most rapidly. Then you will find that much of the computation can be placed in the outer loop.

% Old code
> %%%psi : 400x2x3350 deltax=deltay=1 ph_Tx ,ph_Rx : 1x3350 zeta:2x3350
> %%%%%code 1
> for ii = 1 : 400
>     for ll = 1 : 3350
>          gamma(ii,1,ll) = sinc(zeta(1,ll)*deltax*cos(ph_Tx(ll))/pi)*            sinc(zeta(1,ll)*deltay*sin(ph_Tx(ll))/pi)*psi(ii,1,ll);
> 
>          gamma(ii,2,ll) = sinc(zeta(2,ll)*deltax*cos(ph_Rx(ll))/pi)* sinc(zeta(2,ll)*deltay*sin(ph_Rx(ll))/pi)*psi(ii,2,ll);
> 
>     end
> end

% New code
for ll = 1:3350
    par1 = sinc(zeta(1,ll)*deltax*cos(ph_Tx(ll))/pi)*...
            sinc(zeta(1,ll)*deltay*sin(ph_Tx(ll))/pi);
    par2 = sinc(zeta(2,ll)*deltax*cos(ph_Rx(ll))/pi)*...
            sinc(zeta(2,ll)*deltay*sin(ph_Rx(ll))/pi);

    for ii = 1:400
         gamma(ii,1,ll) = par1*psi(ii,1,ll);
         gamma(ii,2,ll) = par2*psi(ii,2,ll);
    end
end

But now we see the inner loop is not necessary
%    for ii = 1:400
%       gamma(ii,1,ll) = par1*psi(ii,1,ll);
%         gamma(ii,2,ll) = par2*psi(ii,2,ll);
%    end
    gamma(:,1,ll) = par1*psi(:,1,ll);
    gamma(:,2,ll) = par2*psi(:,1,ll);


You can now improve this code further still to obtain the final result.
Hth
Darren
0
3/26/2009 1:18:01 AM
Reply: