Using a 3D matrix to avoid for loops
Show older comments
I would like to speed up my code and avoid for loops. I want to extract smaller matrices which are placed along the diagonal of a bigger matrix and put those into an array. The code looks like this:
SC = zeros(m, n, n);
for i = 1:m
SC(i, 1:n, 1:n) = matrix(n*(i-1)+1:i*n, n*(i-1)+1:i*n);
end
SC = SC(:,:,:);
Do I have to include the for loop or is there a way to extract the matrix all at once?
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!