MATLAB: How to multiply each element of a cell array by a different scalar

cell arraysvector

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?.

Best Answer

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')