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

17 次查看(过去 30 天)
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?.

采纳的回答

Guillaume
Guillaume 2019-8-14
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')

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by