Multiply a cell array with a matrix in an equation

조회 수: 4 (최근 30일)
MarshallSc
MarshallSc 2021년 7월 9일
댓글: MarshallSc 2021년 7월 9일
Hello, I have a cell array like below (let's call it C).
C:
And a 4*4 matrix for example:
A=rand(4,4);
I want to multiply these arguments (multiply the first component of A (A(1,1)) to all the values in the first component of C(C{1})) in an equation such that:
eqn=A*C + (1-A)*C
How can I do this?
I tried for loop but didn't work. I also tried to just use element wise multiplication but didn't work either, I get the error below:
Operator '.*' is not supported for operands of type 'cell'.
  댓글 수: 2
Matt J
Matt J 2021년 7월 9일
I tried for loop but didn't work
Since you haven't shown us the code you tried, we cannot tell you why it didn't work.
MarshallSc
MarshallSc 2021년 7월 9일
편집: MarshallSc 2021년 7월 9일
It's basically writing the equation above:
for i=1:4
for j=1:4
eqn{i,j}=A(i,j).*C{i,j}+(1-A(i,j)).*C{i,j};
end
end

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

채택된 답변

Matt J
Matt J 2021년 7월 9일
Your code gives me no errors, but make sure you pre-allocate an empty cell array for eqn:
eqn=cell(4);
for i=1:4
for j=1:4
eqn{i,j}=A(i,j)*C{i,j}+(1-A(i,j))*C{i,j};
end
end
  댓글 수: 3
Matt J
Matt J 2021년 7월 9일
eqn{i,j}=A{i,j}.*C{i,j}+(1-A{i,j}).*C{i,j};
MarshallSc
MarshallSc 2021년 7월 9일
Thank you!

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

추가 답변 (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