MATLAB: I have 60*3 Matrix. i have store it into L. then i have to allocate these value as (L1, L2, L3, L4…….L20) for 20 matrix. how can i allocate. please tell me command or allocation

advanced loopfor loopMATLAB

Best Answer

If you are trying to create 20 different variables named L1, L2, ..., L20, this is a bad idea. It will be difficult to write good code downstream and will be much harder to maintain. Instead, consider organizing your data into slices that can be indexed. E.g., one method using cell arrays:
LC = mat2cell(L,3*ones(20,1),3);
Then downstream in your code you would use LC{1} instead of L1, and LC{2} instead of L2, etc. That way you can write indexed loops to use these matrices.
Another way would be to organize the data into a 3D array where the first 2D slices are your 3x3 matrices. E.g.,
LA = permute(reshape(L',3,3,20),[2 1 3]);
Then downstream in your code you would use LA(:,:,1) instead of L1, LA(:,:,2) instead of L2, etc. Again, this allows you to write indexed loops to use these matrices. This storage scheme also allows you to use some of the nD matrix algebra packages in the FEX.