矢量化4嵌套for循环

时间:2015-06-01 22:29:49

标签: matlab vectorization

我正在尝试向量化2个内部嵌套for循环,但我无法想出办法来做到这一点。已编写FS1FS2函数来接受N_thetaN_e的参数,这是循环迭代的参数

%% generate regions
for raw_r=1:visual_field_width
    for raw_c=1:visual_field_width       
        r = raw_r - center_r;
        c = raw_c - center_c;

        % convert (r,c) to polar: (eccentricity, angle)
        e = sqrt(r^2+c^2)*deg_per_pixel;
        a = mod(atan2(r,c),2*pi);

        for nt=1:N_theta
            for ne=1:N_e
                regions(raw_r, raw_c, nt, ne) = ...
                    FS_1(nt-1,a,N_theta) * ...
                    FS_2(ne-1,e,N_e,e0_in_deg, e_max);
            end
        end
    end
end

理想情况下,我可以通过以下方式替换两个内部嵌套for循环:

regions(raw_r,raw_c,:,:) = FS_1(:,a,N_theta) * FS_2(:,N_e,e0_in_deg,e_max);

但这是不可能的。也许我错过了一个简单的修复或矢量化技术? e0_in_dege_max是参数。

FS_1功能

function h = FS_1(n,theta,N,t)

if nargin==2
    N = 9;
    t=1/2;
elseif nargin==3
    t=1/2;
end

w = (2*pi)/N;

theta = theta + w/4;

if n==0 && theta>(3/2)*pi
    theta = theta - 2*pi;
end
h = FS_f((theta - (w*n + 0.5*w*(1-t)))/w);

FS_2功能

function g = FS_gne(n,e,N,e0, e_max)

if nargin==2
    N = 10;
    e0 = .5;
elseif nargin==3
    e0 = .5;
end


w = (log(e_max) - log(e0))/N;
g = FS_f((log(e)-log(e0)-w*(n+1))/w);

并且FS_f函数是

function f = FS_f(x, t)

if nargin<2
    t = 0.5;
end

f = zeros(size(x));

% case 1
idx = x>-(1+t)/2 & x<=(t-1)/2;
f(idx) = (cos(0.5*pi*((x(idx)-(t-1)/2)/t))).^2;

% case 2
idx = x>(t-1)/2 & x<=(1-t)/2;
f(idx) = 1;

% case 3
idx = x>(1-t)/2 & x<=(1+t)/2;
f(idx) = -(cos(0.5*pi*((x(idx)-(1+t)/2)/t))).^2+1;

1 个答案:

答案 0 :(得分:0)

我必须假设常量的值,然后使用ndgrid来查找可能的配置,并使用sub2ind来获取索引。这样做我删除了所有循环。如果这产生了正确的值,请告诉我。

function RunningFunction
    %% generate regions

    visual_field_width = 10;
    center_r = 2;
    center_c = 3;

    deg_per_pixel = 17;
    N_theta = 2;
    N_e = 5;
    e0_in_deg = 35;
    e_max = 17;

    [raw_r, raw_c, nt, ne] = ndgrid(1:visual_field_width, 1:visual_field_width, 1:N_theta, 1:N_e);
    ind = sub2ind(size(raw_r), raw_r, raw_c, nt, ne);

    r = raw_r - center_r;
    c = raw_c - center_c;

    % convert (r,c) to polar: (eccentricity, angle)
    e = sqrt(r.^2+c.^2)*deg_per_pixel;
    a = mod(atan2(r,c),2*pi);

    regions(ind) = ...
        FS_1(nt-1,a,N_theta) .* ...
        FS_2(ne-1,e,N_e,e0_in_deg, e_max);

    regions = reshape(regions, size(raw_r));

end

function h = FS_1(n,theta,N,t)
    if nargin==2
        N = 9;
        t=1/2;
    elseif nargin==3
        t=1/2;
    end

    w = (2*pi)./N;

    theta = theta + w/4;

    theta(n==0 & theta>(3/2)*pi) = theta(n==0 & theta>(3/2)*pi) - 2*pi;

    h = FS_f((theta - (w*n + 0.5*w*(1-t)))/w);
end

function g = FS_2(n,e,N,e0, e_max)

if nargin==2
    N = 10;
    e0 = .5;
elseif nargin==3
    e0 = .5;
end


w = (log(e_max) - log(e0))/N;
g = FS_f((log(e)-log(e0)-w*(n+1))/w);
end

function f = FS_f(x, t)
    if nargin<2
        t = 0.5;
    end

    f = zeros(size(x));

    % case 1
    idx = x>-(1+t)/2 & x<=(t-1)/2;
    f(idx) = (cos(0.5*pi*((x(idx)-(t-1)/2)/t))).^2;

    % case 2
    idx = x>(t-1)/2 & x<=(1-t)/2;
    f(idx) = 1;

    % case 3
    idx = x>(1-t)/2 & x<=(1+t)/2;
    f(idx) = -(cos(0.5*pi*((x(idx)-(1+t)/2)/t))).^2+1;
end