I have a vendetta against for loops.
I'm trying to remove them all from a particular project but I've hit a roadblock.
Below is part of the code for an implementation of the color fast guided filter:
a = zeros(N, 3);
for q = 1:N
sigma = [var_I_rr(q), var_I_rg(q), var_I_rb(q);
var_I_rg(q), var_I_gg(q), var_I_gb(q);
var_I_rb(q), var_I_gb(q), var_I_bb(q)];
cov_Ip = [cov_Ip_r(q), cov_Ip_g(q), cov_Ip_b(q)];
a(q,:) = cov_Ip / (sigma + eps * eye(3));
In essence we've got a 1x3 vector being multiplied by the inverse of a 3x3 matrix, n number of times.
The following attempt obiously doesn't work because 'A/B' or 'A*inv(B)' is only defined for 2-D matrices, but hopefully it illustrates what I'm trying to do.
top = cat(2,reshape(var_I_rr,1,1,),reshape(var_I_rg,1,1,),reshape(var_I_rb,1,1,));
mid = cat(2,reshape(var_I_rg,1,1,),reshape(var_I_gg,1,1,),reshape(var_I_gb,1,1,));
bot = cat(2,reshape(var_I_rb,1,1,),reshape(var_I_gb,1,1,),reshape(var_I_bb,1,1,));
sigma = cat(1,top,mid,bot);
cov_Ip = cat(2,reshape(cov_Ip_r,1,1,),reshape(cov_Ip_g,1,1,),reshape(cov_Ip_b,1,1,));
eps = repmat(eps*reshape(eye(3),3,3,1),1,1,N);
b = cov_Ip / (sigma+eps);
If sigma+eps was already inverted (along the first two dimensions) I could do the following:
b = squeeze(pagemtimes(cov_Ip, invSigma))';
To illustrate the problem more clearly, here's simpler example of what I'm effectively trying to vectorize.
for i = 1:N
a = A(:,:,i);
b = B(:,:,i);
c = a / b;
C(i,:) = c;
Is there any way to vectorize this. Is it possible to invert the first two dimensions of a 3-D matrix independently over the last dimension without using a for loop? Like 'pagemtimes' but for division/inverting.