MATLAB: Conv a matrix multiple times

MATLABmatricesmatrixmatrix arraymatrix manipulation

Hello
I have a rectangular matrix and i want to convolve it to some power of that matrix times. e.g.
a = [1 1 0; 1 0 1]^5
convolve the 1st row with the 1st row 5 times and then the row with the 2nd row 5 times.
Any help!

Best Answer

a1 = [1 1 0;
1 0 1]; % rand(2,3);
p = 5; % power
[m,n] = size(a1);
%% Method 1, straighforward double for-loops
ap = a1;
for p=2:p
temp = zeros(m,n+size(ap,2)-1);
for i=1:m
temp(i,:) = conv(a1(i,:),ap(i,:));
end
ap = temp;
end
ap
%% Method 2 single for-loop
ap = a1;
i = 0:n-1;
k = n;
A1 = reshape(a1,[m 1 n]);
for p=2:p
c = i+(1:k)';
[r,c] = ndgrid(1:m,c(:));
ap = A1.*ap;
k = n+k-1;
ap = accumarray([r(:) c(:)], ap(:), [m, k]);
end
ap
You might try to improve by using a "smart" of power raising, by using the binary decomposition of p.