Multiply two cell arrays

5 views (last 30 days)
lucksBi
lucksBi on 28 Feb 2018
Commented: Stephen23 on 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 Comments
lucksBi
lucksBi on 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 on 28 Feb 2018
lucksBi: I did not ask you for the size of mul1 or mul2. Please read my comment again.

Sign in to comment.

Accepted Answer

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

Sign in to comment.

More Answers (0)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!