# Covert a cell with the same indices into individual matrices

1 view (last 30 days)
MarshallSc on 7 Jul 2021
Commented: MarshallSc on 7 Jul 2021
Hello, does anyone how I can convert a 10*10 cell array that each contains a 4*4 matrix into individual matrices. For example considering the code below:
x1 = rand(10,10);
y1 = rand(10,10);
z1 = rand(10,10);
r1 = rand(10,10);
a1 = cat(3,x1,y1,z1,r1);
x2 = rand(10,10);
y2 = rand(10,10);
z2 = rand(10,10);
r2 = rand(10,10);
a2 = cat(4,x2,y2,z2,r2);
tmp = num2cell(sqrt(a1./a2),3:4);
fun = @(a)reshape(a,4,4);
out = cellfun(fun,tmp,'uni',0);
What I'm looking to do is to convert each components of the cell with the same index into a seperate matrix, so that at the end I would have 16 seperate 10*10 matrices. For example considering all the (1,1) of the cells (out{1}(1,1), out{2}(1,1), out{3}(1,1), ...). Can one for loop be used to this conversion that at the end 16 different 10*10 matrices will be produced? Or is it better to change the method to calculate the result of the above code (out) from the beginning so that the matrices are obtained themselves? Thank you.

Stephen23 on 7 Jul 2021
It is not clear to me why you need to split the numeric data into 100 4x4 matrices, just to recombine the numeric data into 16 10x10 matrices. Why not skip the middle entirely, and go straight to the 16 10x10 matrices?
x1 = rand(10,10);
y1 = rand(10,10);
z1 = rand(10,10);
r1 = rand(10,10);
a1 = cat(3,x1,y1,z1,r1);
x2 = rand(10,10);
y2 = rand(10,10);
z2 = rand(10,10);
r2 = rand(10,10);
a2 = cat(4,x2,y2,z2,r2);
out2 = reshape(num2cell(sqrt(a1./a2),1:2),4,4) % <- try this
out2 = 4×4 cell array
{10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double}
cmp2 = out2{4,1} % for comparison
cmp2 = 10×10
2.9656 0.7360 0.9425 1.2257 0.4062 1.3241 0.8456 0.8951 1.8988 1.1643 0.8959 0.8702 2.7521 2.4620 1.2179 1.2658 1.2211 0.7416 1.2571 1.7134 2.3034 1.2035 1.2259 1.4452 0.8945 1.5588 1.6855 1.0448 0.3416 1.0999 0.6387 0.7931 0.3847 1.1504 1.3599 0.9472 0.9657 0.7773 2.4375 0.4988 12.2880 1.0568 0.3826 1.0354 0.3258 2.1765 0.4227 0.9767 0.5842 0.8154 3.5563 1.5147 1.8729 0.8701 0.8464 0.5248 3.8636 1.4231 0.5179 0.3991 1.0273 1.6911 1.0084 1.0902 1.1016 0.8318 1.2163 1.4484 0.9842 0.7283 5.0321 1.3926 0.9870 0.6820 1.3326 1.2969 1.0028 0.7557 0.4904 1.8180 0.9030 1.1801 1.0515 0.4170 1.5448 1.3636 0.5483 0.2722 0.1757 0.7456 0.2963 1.1006 0.6940 1.9821 0.7103 1.0029 0.3302 1.2735 1.1195 1.0808
Comparing against your very indirect approach:
tmp = num2cell(sqrt(a1./a2),3:4);
fun = @(a)reshape(a,4,4);
out1 = cellfun(fun,tmp,'uni',0)
out1 = 10×10 cell array
{4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double} {4×4 double}
cmp1 = cellfun(@(m)m(4,1),out1) % for comparison
cmp1 = 10×10
2.9656 0.7360 0.9425 1.2257 0.4062 1.3241 0.8456 0.8951 1.8988 1.1643 0.8959 0.8702 2.7521 2.4620 1.2179 1.2658 1.2211 0.7416 1.2571 1.7134 2.3034 1.2035 1.2259 1.4452 0.8945 1.5588 1.6855 1.0448 0.3416 1.0999 0.6387 0.7931 0.3847 1.1504 1.3599 0.9472 0.9657 0.7773 2.4375 0.4988 12.2880 1.0568 0.3826 1.0354 0.3258 2.1765 0.4227 0.9767 0.5842 0.8154 3.5563 1.5147 1.8729 0.8701 0.8464 0.5248 3.8636 1.4231 0.5179 0.3991 1.0273 1.6911 1.0084 1.0902 1.1016 0.8318 1.2163 1.4484 0.9842 0.7283 5.0321 1.3926 0.9870 0.6820 1.3326 1.2969 1.0028 0.7557 0.4904 1.8180 0.9030 1.1801 1.0515 0.4170 1.5448 1.3636 0.5483 0.2722 0.1757 0.7456 0.2963 1.1006 0.6940 1.9821 0.7103 1.0029 0.3302 1.2735 1.1195 1.0808
isequal(cmp1,cmp2)
ans = logical
1
MarshallSc on 7 Jul 2021
Thanks a lot Stephen for your kind help, you’re awesome!