Covert a cell with the same indices into individual matrices
4 visualizaciones (ú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
Stephen23
el 7 de Jul. de 2021
Editada: Stephen23
el 7 de Jul. de 2021
"Also, the output of out2 is givng me a 4*4 cell array, shouldn't be a 16*16 cell array..."
You have a sum total of
10*10*4*4
numeric elements (arranged in 100 4x4 matrices). If we divide those elements into 10x10 matrices, then you will have 16 said matrices... which can be arranged in a 4x4 cell array. It is not clear how you want to duplicate/interpolate/extrapolate/whatever 1600 numeric elements in order to get
10*10*16*16
numeric elements.
"...(the result that I'm looking to get)?"
The result you requested was "...at the end 16 different 10*10 matrices will be produced...".
As far as I am aware, 4x4 == 16.
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrices and Arrays 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!