extracting diagonals from 3d matrices

I try to extract diagonals from 7*7*217 matrices into 1*7*217. How can i make it? I tried below code but it did't work. I would appreciate if you help me.
for k=1:217
D(:,:,k)=diag(diag(L(:,:,k)));
end

Answers (1)

Matt J
Matt J on 4 Mar 2018
Edited: Matt J on 4 Mar 2018
Lr=reshape(L,1,49,[]);
D=Lr(1,1:8:end,:);

Categories

Asked:

on 4 Mar 2018

Edited:

on 4 Mar 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!