How to multiply data from cells?

조회 수: 7 (최근 30일)
Abirami
Abirami 2015년 1월 28일
댓글: Giorgi 2015년 1월 28일
Hello,
Consider I have a cell array of size (1x20). I want to multiply all the elements of the cell and display it as a single output. For eg:
X= 1 2 7 7 8 9 6 7 8 9 2 1 3 1 4 6 7 4 5 3
I want my output to be stored in a new variable say 'a'
a=1,290,482,565,120.
How do i do it? Please help. Thanks in advance.
  댓글 수: 1
Guillaume
Guillaume 2015년 1월 28일
편집: Guillaume 2015년 1월 28일
The proper syntax for declaring a cell array is:
X = {1 2 7 7 8 9 6 7 8 9 2 1 3 1 4 6 7 4 5 3};
If you don't use the right syntax in your examples, then we're left wondering what you really mean.
So the first question is: why are you using a cell array? In this particular example, a plain matrix would be sufficient.
Second question, the product of a bunch of numbers is just one single number, so what are the multiple values in your second cell array (or is a matrix?)?
edit: I've just worked out that your a is a scalar and you've just separated the thousands by a comma. It is just so much clearer, if you use proper matlab syntax throughout your question.

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

채택된 답변

Guillaume
Guillaume 2015년 1월 28일
편집: Guillaume 2015년 1월 28일
There is no point in having a cell array of scalar double, a matrix is more efficient.
Your cell array can be converted into a matrix / vector with cell2mat. To calculate the product of the elements of a vector, you use prod. So:
X= {1 2 7 7 8 9 6 7 8 9 2 1 3 1 4 6 7 4 5 3};
a = prod(cell2mat(X))
  댓글 수: 1
Giorgi
Giorgi 2015년 1월 28일
Well you are right my solution was wrong :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by