Multiply two cell arrays

7 visualizzazioni (ultimi 30 giorni)
lucksBi
lucksBi il 28 Feb 2018
Commentato: Stephen23 il 28 Feb 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 Commenti
lucksBi
lucksBi il 28 Feb 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 il 28 Feb 2018
lucksBi: I did not ask you for the size of mul1 or mul2. Please read my comment again.

Accedi per commentare.

Risposta accettata

Birdman
Birdman il 28 Feb 2018
Try this:
res=cellfun(@times,mul1,mul2,'uni',0)
  3 Commenti
Andrei Bobrov
Andrei Bobrov il 28 Feb 2018
Modificato: Andrei Bobrov il 28 Feb 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 il 28 Feb 2018
It should not. It worked for me.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Multidimensional Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by