This question already has an answer here:
- Extract diagonal element from each frontal slice of tensor 3 answers
I want to extract diagonals of a 3D matrix (Sigma below) into another 3D matrix (Sigma2 below).
Sigma = repmat(magic(4),1,1,3);
Sigma2 = nan(1,4,3);
for i=1:3
Sigma2(1,:,i) = diag(Sigma(:,:,i));
end
Is there a more efficient way for doing this?