I struggle to create a sparse matrix efficiently in matlab.
The problem looks like this:
Let's take a square matrix
a = 4; b = 2;
t = rand(a,a);
What I want is equivalent to P = sparse(kron(eye(a),ones(1,b)).*repmat(t,1,b))
Only that will be a = 12*2^10 and b = 2^10 (i.e. first creating the element wise multiplication between the kroneker product and the repmat of t is too memory intensive).
I've been browsing around and couldn't find a related question. My idea is to implement this matrix directly using the sparse(B,d,m,n) or spidag() syntax.
Would be amazing if someone had an idea.
Cheers
I expect a matrix like this:
P = sparse(kron(eye(a),ones(1,b)).*repmat(t,1,b))
Tested with your example and
b=3.Logic:
ris the row index, which is the same for the sparse output and row input fromt; each row repeatedbtimes.cis the column index withintto take the value from. This appears to start as1:b, and then take the nextbelements starting from the following column, wrapping round to column 1.Then you can directly form the sparse matrix extracting elements from
tusing a linear index fromrandc.Output: