Multiply a cell array with a matrix in an equation

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 日

1 投票

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 件のコメント

MarshallSc
MarshallSc 2021 年 7 月 9 日
編集済み: MarshallSc 2021 年 7 月 9 日
Just for my own information, what if instead of a matrix, “A” was a cell array with the same size, how would the multiplication be performed? I know it can be converted by using cell2mat.
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 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

質問済み:

2021 年 7 月 9 日

コメント済み:

2021 年 7 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by