Multiply a cell array with a scalar

Hi, I have a cell array that contain a number of different sized matrices. I need to multiply each of these cell elements, where the number of cells is not constant, with a single scalar value. Does anyone know how to do this? It does not work with the standard call of cellfun since the cells are of different size,
cellfun(@times,a,{2})
The reason is that I use matlab as an interface to create stuff to another program and I depending on what item I create I need to use different tones on the colormaps.

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 2 月 10 日
編集済み: Azzi Abdelmalek 2014 年 2 月 10 日

14 投票

yournumber=2;
a={[1 2 3],[2 5;6 8], [2 5 8 7]}
cellfun(@(x) x*yournumber,a,'un',0)

4 件のコメント

Patrik Ek
Patrik Ek 2014 年 2 月 10 日
Thanks, exactly what I was looking for
Tai Doan
Tai Doan 2016 年 6 月 8 日
Can you explain the meaning of using 'un' and 0 at the end of this line please? Do I have to use it?
cellfun(@(x) x*yournumber,a,'un',0)
Andrew Bliss
Andrew Bliss 2016 年 8 月 4 日
編集済み: Andrew Bliss 2016 年 8 月 4 日
'un',0 is a shorthand way of writing 'UniformOutput',false See the help for what that means - in essence it will make cellfun output a cell rather than an array.
swenia
swenia 2020 年 2 月 4 日
編集済み: swenia 2020 年 2 月 4 日
This is very helpful, thanks. Follow-up question: what if I'd like to multiply each cell element with a different number stored in a vector?
For instance:
a = {[1 2; 3 4], [5 6; 7 8]}
b = [0;1]
I'd like to multiply a and b such that the result will be:
ans = {[0 0; 0 0], [5 6; 7 8]}

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

質問済み:

2014 年 2 月 10 日

編集済み:

2020 年 2 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by