how to multiply each element of a cell array by a different scalar

33 visualizzazioni (ultimi 30 giorni)
I have a cell array V={[1 2 3;4 5 6],[0 0 0;1 2 9],[0 9 3;2 4 6]} and a vector a=[2 3 4]. I want to multiply the first cell elements of V by the first element of a, i.e., [1 2 3;4 5 6] 2 , the second cell elements of V by the second element of a, i.e., [0 0 0;1 2 9]3, etc. How can I do that?.

Risposta accettata

Guillaume
Guillaume il 14 Ago 2019
The easiest would be:
result = cellfun(@times, V, num2cell(a), 'UniformOutput', false)
or just use a loop.
Note that V and a must have the same size:
assert(isequal(size(V), size(a)), 'sizes not equal')

Più risposte (0)

Categorie

Scopri di più su Multidimensional Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by