Multiply two cell arrays

5 visualizaciones (últimos 30 días)
lucksBi
lucksBi el 28 de Feb. de 2018
Comentada: Stephen23 el 28 de Feb. de 2018
Hi i have following two cell arrays mul1 and mul2.
I want to multiply each cell of mul1 with corresponding cell of mul2. I am using following code
for i=8
for j=7
mul3{i,j}= mul1{i,j}.*mul2{i,j};
end
end
but it gives error:
Error using .*
Matrix dimensions must agree.
i also tried without using loop but still same issue.
Please help
  3 comentarios
lucksBi
lucksBi el 28 de Feb. de 2018
yes you are right. It multiplies values within cells which are numeric values. Size of mul1 is 8x7 cell and mul2 is 8x7 cell.
Result of size is:
for i=1:8
for j=1:7
a= size(mul1{i,j})
b= size(mul2{i,j})
mul3{i,j}= mul1{i,j}.*mul2{i,j};
end
end
Result:
a = 1 0
b = 0 0
Stephen23
Stephen23 el 28 de Feb. de 2018
lucksBi: I did not ask you for the size of mul1 or mul2. Please read my comment again.

Iniciar sesión para comentar.

Respuesta aceptada

Birdman
Birdman el 28 de Feb. de 2018
Try this:
res=cellfun(@times,mul1,mul2,'uni',0)
  3 comentarios
Andrei Bobrov
Andrei Bobrov el 28 de Feb. de 2018
Editada: Andrei Bobrov el 28 de Feb. de 2018
@lucksBi: All right!
>> mul1
mul1 =
8×7 cell array
[1×0 double] [ -0.1140] [ -0.2700] [1×2 double] [1×2 double] [] []
[1×0 double] [1×0 double] [1×3 double] [ 0.1967] [1×0 double] [1×2 double] [1×4 double]
[ -0.1140] [1×0 double] [1×0 double] [1×3 double] [1×2 double] [1×0 double] [1×3 double]
[ -0.2700] [1×3 double] [1×0 double] [1×0 double] [ 0.2598] [ -0.8066] [1×3 double]
[1×2 double] [ 0.1967] [1×3 double] [1×0 double] [1×2 double] [ -0.0151] [1×4 double]
[1×2 double] [1×0 double] [1×2 double] [ 0.2598] [1×2 double] [1×0 double] [1×2 double]
[1×0 double] [1×2 double] [1×0 double] [ -0.8066] [ -0.0151] [1×0 double] [1×2 double]
[ -0.0974] [1×4 double] [1×3 double] [1×3 double] [1×4 double] [1×2 double] [1×2 double]
>> mul2
mul2 =
8×7 cell array
[] [ 1] [ 1] [1×2 double] [1×2 double] [] []
[] [] [1×3 double] [ 1] [] [1×2 double] [1×4 double]
[ 1] [] [] [1×3 double] [1×2 double] [] [1×3 double]
[ 1] [1×3 double] [] [] [ 1] [ 1] [1×3 double]
[1×2 double] [ 1] [1×3 double] [] [1×2 double] [ 1] [1×4 double]
[1×2 double] [] [1×2 double] [ 1] [1×2 double] [] [1×2 double]
[] [1×2 double] [] [ 1] [ 1] [] [1×2 double]
[ 1] [1×4 double] [1×3 double] [1×3 double] [1×4 double] [1×2 double] [1×2 double]
>> cellfun(@times,mul1,mul2,'un',0)
ans =
8×7 cell array
[] [ -0.1140] [ -0.2700] [1×2 double] [1×2 double] [] []
[] [] [1×3 double] [ 0.1967] [] [1×2 double] [1×4 double]
[ -0.1140] [] [] [1×3 double] [1×2 double] [] [1×3 double]
[ -0.2700] [1×3 double] [] [] [ 0.2598] [ -0.8066] [1×3 double]
[1×2 double] [ 0.1967] [1×3 double] [] [1×2 double] [ -0.0151] [1×4 double]
[1×2 double] [] [1×2 double] [ 0.2598] [1×2 double] [] [1×2 double]
[] [1×2 double] [] [ -0.8066] [ -0.0151] [] [1×2 double]
[ -0.0974] [1×4 double] [1×3 double] [1×3 double] [1×4 double] [1×2 double] [1×2 double]
>>
Birdman
Birdman el 28 de Feb. de 2018
It should not. It worked for me.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Multidimensional Arrays 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