multiply/divide two cells array

조회 수: 23 (최근 30일)
shobhit mehrotra
shobhit mehrotra 2015년 4월 15일
댓글: Renato Agurto 2015년 4월 15일
I have two cell arrays that have the exact same amount of elements
A: cell 1: [2, 5, 8, 9]
cell 2: [ 5, 7, 3]
cell 3: [ 5, 3]
B: cell 1: [ 1 ,8, 6, 3]
cell 2: [ 4, 2, 7]
cell 3: [ 8, 9]
I want to multiply each element of A by B, (cell 1= [2 40 48 27] ) so I get a new cell array C
I have this
C= cellfun(@x, A(x)*B(x), 'UniformOutput',false)
Thanks!

채택된 답변

Renato Agurto
Renato Agurto 2015년 4월 15일
Hi, you need a 2 input function:
C= cellfun(@(x,y) x*y, A,B, 'UniformOutput',false)
Alternatively:
C = num2cell(cell2mat(A).*cell2mat(B))
I hope this helps
  댓글 수: 2
Dishant Arora
Dishant Arora 2015년 4월 15일
It should be a dot product:
C= cellfun(@(x,y) x.*y, A,B, 'UniformOutput',false)
Renato Agurto
Renato Agurto 2015년 4월 15일
You'are right. I though it was a a cell array with single double elements.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by