Build determinantes of matrices in cell arrays

4 visualizaciones (últimos 30 días)
Ahmed Hossam
Ahmed Hossam el 19 de Abr. de 2017
Comentada: Jan el 19 de Abr. de 2017
I have a cell array X with dimension (N x 2) and N is an even number. This cell array is filled with (2 x 2) Matrices of type double.
I'm wondering, if there is a way to build the determinants of each of those matrices (type: double) in a vectorized manner without using a for loop.
My solution was:
Y = zeros(N,1)
for i = 1:N
Y = det(X{i});
end
Now I have this Y of size (N x 1), which will contain all the determinants of the (2 x 2) matrices stored in the cell array X with size (N x 2).
Can I "vectorize" the for loop in some way or the other?
Thanks for having a look at this!

Respuesta aceptada

KSSV
KSSV el 19 de Abr. de 2017
doc cellfun
N = 4 ;
X = cell(N,2) ;
for i = 1:N
X{i,1} = rand(2) ;
X{i,2} = rand(2) ;
end
detX = cellfun(@det,X) ;
  5 comentarios
Stephen23
Stephen23 el 19 de Abr. de 2017
Editada: Stephen23 el 19 de Abr. de 2017
@Ahmed Hossam: it is seldom required to calculate a matrix inverse. If you are solving systems of equations, then MATLAB has much better methods available. Read the inv help carefully to know what the "more effective" methods are.
Jan
Jan el 19 de Abr. de 2017
While the cellfun method is nice, it is not "vectorized" actually. You only do not see the loop anymore. Internally you cannot treat the cell elements in a vectorized way.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by