1
votes

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
1
I cant find a question or problem ? - Irreducible
@Irreducible The code isn't vectorised in its current state. I'm wondering how I'd vectorise it. - Jason Tracey
As you have added a question, you should tell us what you have tried to do so/ why you are failing in implementing it. - Irreducible
I am failing to implement it as my understanding of matrix manipulation is limited. If the desired values to be inserted were scalar, I would not have an issue. There does not appear to be an intuitive way to achieve my desired results without the use of for-loops. I am investigating sparse matrices, however these are quite complex and I am struggling to understand how I would use them in this case. - Jason Tracey

1 Answers

2
votes

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