Pregunta

I need to fill in a matrix (size_out,size_in). I've looking for similar problems but none of their solutions could help me out.

This was my first attempt

for k= 0:size_out-1
    for n= 0:size_in-1
        part1= sincd(2*No-2, 2*size_in, (k+1/2)/factor -n -1/2);
        part3= sincd(2*No-2, 2*size_in, (k+1/2)/factor +n +1/2);
        part2= cos( (pi/(2*size_in) ) * ( (k+1/2)/factor -n -1/2) );
        part4= cos( (pi/(2*size_in) ) * ( (k+1/2)/factor +n +1/2) );
        A(k+1,n+1)= part1*part2+part3*part4;
    end
end

I vectorized this code by eliminating the inner loop:

for k= 0:size_out-1
    A(k+1,1:size_in)= ...
        sincd(2*No-2, 2*size_in, (k+1/2)/factor -(0:size_in-1) -1/2 ) .* ...
        cos( pi/(2*size_in) * ( (k+1/2)/factor -(0:size_in-1) -1/2 ) ) + ...
        sincd(2*No-2, 2*size_in, (k+1/2)/factor +(0:size_in-1) +1/2 ) .* ...
        cos( pi/(2*size_in) * ( (k+1/2)/factor +(0:size_in-1) +1/2 ) );
end

My question is: How to vectorize the outer loop?

I'm not sure if the combination of reshape&permute or bsxfun could help here.

Thanks in advance.

¿Fue útil?

Solución

Since a few parameters and functions were left undefined I took the liberty of defining them.

What's nice is to see the very nice speed up by the 'vectorization' - though much of its probably MATLAB parallelizing. This is a bit of an abuse on kron, but here's an approach.

speed up plot

Note the for loop here is to test for various scales

% // testing for a range of scales
t1 = [];
t2 = [];
scales = floor(logspace(1,3,20));
for scale = scales

    % // Some guessed parameters and large sizes
    size_out = scale;
    size_in = scale;
    No = 2; %?
    factor = 3;

    % // Arbitrary function for sincd
    sincd = @(x, y, z) x.*y.*z;

    tic
    % // Provided code
    A = zeros(size_out,size_in);
    for k= 0:size_out-1
        for n= 0:size_in-1
            part1= sincd(2*No-2, 2*size_in, (k+1/2)/factor -n -1/2);
            part3= sincd(2*No-2, 2*size_in, (k+1/2)/factor +n +1/2);
            part2= cos( (pi/(2*size_in) ) * ( (k+1/2)/factor -n -1/2) );
            part4= cos( (pi/(2*size_in) ) * ( (k+1/2)/factor +n +1/2) );
            A(k+1,n+1)= part1*part2+part3*part4;
        end
    end
    t1 = [t1; toc];



    tic

Here I use a kronecker tensor product to build two matrices containing the row and column indexes followed by an identity matrix so that everything is the same shape going into sincd

    ns = kron([1:size_in]-1,ones(1,size_out)');
    ks = kron(ones(1,size_in),[1:size_out]'-1);
    ident = ones(size_out,size_in);

Here I simply replaced k, n, with ks and ns making sure I keep operations element wise and of the same size

    B =  sincd( 2*No-2*ident, 2*size_in*ident, (ks+1/2)/factor -ns -1/2) ...
        .* cos( (pi/(2*size_in) ) * ( (ks+1/2)/factor -ns -1/2) ) ...
      +  sincd(2*No-2*ident, 2*size_in*ident, (ks+1/2)/factor +ns +1/2) ...
         .*cos( (pi/(2*size_in) ) * ( (ks+1/2)/factor +ns +1/2) );

    t2 = [t2; toc];

    % // Should be zero
    norm(A-B)


end

loglog(scales, t1./t2)
title('speed up')
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top