Multiply each column by a elements of cell array

1 回表示 (過去 30 日間)
lucksBi
lucksBi 2017 年 6 月 13 日
コメント済み: lucksBi 2017 年 6 月 13 日
hey
i have a matrix like this:
matrix=
0 2 4
3 0 0
0 1 0
3 0 1
0 0 0
and cell array: mul={1,1,0.6,0.6,0.3}
i want to multiply all columns by 'mul' and then add. e.g. [0x1 + 3x1 + 0x0.6 + 3x0.6 + 0x0.3] and same for all other columns.
Thanks

採用された回答

KSSV
KSSV 2017 年 6 月 13 日
matrix=[0 2 4
3 0 0
0 1 0
3 0 1
0 0 0];
mul={1,1,0.6,0.6,0.3} ;
iwant = sum(bsxfun(@times,matrix,[mul{:}]'))
  2 件のコメント
lucksBi
lucksBi 2017 年 6 月 13 日
編集済み: lucksBi 2017 年 6 月 13 日
Thanks alot. Will it be same if matrix is a cell array instead of 2D?
KSSV
KSSV 2017 年 6 月 13 日
No...it is better to work with matrices. I suggest to convert your mul to matrix.

サインインしてコメントする。

その他の回答 (2 件)

Guillaume
Guillaume 2017 年 6 月 13 日
Conceptually it would make a lot more sense for your cell array to be a column rather than a row.
Anyway, the easiest way to do what you want is to convert your cell array into a column vector. It is then trivial to multiply it with the matrix:
result = sum(matrix .* cell2mat(mul')); %requires R2016b or later
%in <R2016b: result = sum(bsxfun(@times, matrix, cell2mat(mul')));
  1 件のコメント
lucksBi
lucksBi 2017 年 6 月 13 日
Yes Thanks alot for suggestion but it was kind of requirement. Thanks for your time.

サインインしてコメントする。


Andrei Bobrov
Andrei Bobrov 2017 年 6 月 13 日
result = matrix.'*cat(1,mul{:});
or
result = [mul{:}]*matrix;
  1 件のコメント
lucksBi
lucksBi 2017 年 6 月 13 日
Thank You for helping. @Andrei Bobrov

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by