How can I split one large matrix to struct with several fields?

1 view (last 30 days)
I have matrix M 100 by 20, how can i split it to struct with 10 fields with size 10 by 20?
Thank you.

Accepted Answer

Matt J
Matt J on 22 Mar 2022
Edited: Matt J on 22 Mar 2022
M=rand(100,20);
Mcell=num2cell(permute(reshape(M,[10,10,20]),[1,3,2]),[1,2]);
cell2struct(Mcell, "f"+(1:10),3)
ans = struct with fields:
f1: [10×20 double] f2: [10×20 double] f3: [10×20 double] f4: [10×20 double] f5: [10×20 double] f6: [10×20 double] f7: [10×20 double] f8: [10×20 double] f9: [10×20 double] f10: [10×20 double]

More Answers (0)

Community Treasure Hunt

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

Start Hunting!