我认为这可以通过向量化最内层循环来优化。
input = 2*rand(24,24,3)-1;
theta = 2*rand(26,12,3)-1;
output = zeros(20,20,12); % preallocating
temp = zeros(3,12); % preallocating
for i = 1:20
for j = 1:20
for c = 1:3
temp(c,:) = [1, reshape(input(i:i+4,j:j+4,c),1,25)]*theta(:,:,c);
end
output(i,j,:) = sum(temp);
end
end
你知道怎么做吗?
你可以这样替换你的内部循环:
aux = [ones(1,3); reshape(input(i:i+4,j:j+4,:),25,3)];
theta_concat = reshape(permute(theta, [2 1 3]),12,78);
output(i,j,:) = theta_concat*aux(:);