Covert a cell with the same indices into individual matrices
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
MarshallSc
el 7 de Jul. de 2021
Comentada: MarshallSc
el 7 de Jul. de 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.
0 comentarios
Respuesta aceptada
Stephen23
el 7 de Jul. de 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
cmp2 = out2{4,1} % for comparison
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)
cmp1 = cellfun(@(m)m(4,1),out1) % for comparison
isequal(cmp1,cmp2)
3 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Numeric Types en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!