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

27 vues (au cours des 30 derniers jours)
mohamed Faraj
mohamed Faraj le 14 Août 2019
Commenté : mohamed Faraj le 14 Août 2019
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?.

Réponse acceptée

Guillaume
Guillaume le 14 Août 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')

Plus de réponses (0)

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by