MATLAB: Multiplying rows of matrix without bsxfun or for loops

bsxfunmatrix multiplicationnon-singleton dimensions

I have two matrices A and B generated as follows:
x = 2;
y = 4;
A = kron(eye(x), ones(1,y));
littleB = horzcat(zeros(y-1,1),eye(y-1));
B = repmat(littleB, [1,x]);
I now want to multiply (element-wise) each row of A with each row of B to get a matrix output like:
0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1
I want to avoid the use of for-loops. I tried to use bsxfun but get an error (Non-singleton dimensions of the two input arrays must match each other), which I understand is caused by the fact that A has two rows (if A was just one row this would work). What would be the best way to achieve what I am trying to do? Any help would be much appreciated.

Best Answer

a = size(A,1);
b = size(B,1);
C = A(repmat(1:a,b,1),:).*repmat(B,a,1);