Jason Tracey
Jason Tracey

Reputation: 106

Vectorisation of code for insertion of n x n matrices in a 3D array along the diagonal of a large matrix

As in my earlier question I am trying to insert small square matrices along the diagonal of a large matrix. However, these matrices are now contained in a 3D array, and have different values. As before, overlapping values are to be added, and the small matrices are only inserted where they can fit fully inside the large matrix. The step dimension will always be equal to 1.

I have achieved an answer through the use of for-loops, but am attempting to vectorise this code for efficiency. How would I do this? The current, unvectorised code is shown below.

function M  = TestDiagonal2()

N           = 10;
n           = 2;
maxRand     = 3;

deepMiniM   = randi(maxRand,n,n,N+1-n);
M           = zeros(N);

for i = 1:N+1-n
    M(i:i+n-1,i:i+n-1) = M(i:i+n-1,i:i+n-1) + deepMiniM(:,:,i);
end

end

The desired result is an NxN matrix with n+1 diagonals populated:

     3     1     0     0     0     0     0     0     0     0
     4     5     3     0     0     0     0     0     0     0
     0     3     3     3     0     0     0     0     0     0
     0     0     1     6     3     0     0     0     0     0
     0     0     0     4     4     4     0     0     0     0
     0     0     0     0     2     3     2     0     0     0
     0     0     0     0     0     2     6     2     0     0
     0     0     0     0     0     0     4     2     2     0
     0     0     0     0     0     0     0     3     3     1
     0     0     0     0     0     0     0     0     3     3

Upvotes: 1

Views: 68

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112679

This makes use of implicit expansion, as well as sparse to add values at coincident indices, and (:) indexing to linearize a matrix in the usual column-major order.

ind1 = repmat((1:n).', n, 1) + (0:N-n); % column indices for the sum
ind2 = repelem((1:n).', n) + (0:N-n); % row indices for the sum
M = full(sparse(ind1(:), ind2(:), deepMiniM(:), N, N)); % sum over those indices

Upvotes: 2

Related Questions