help I need matlab code that multiply each element in each coulmn by a each element in avector with the same length

help I need matlab code that multiply each element in each coulmn by a each element in avector with the same length

 Accepted Answer

Use bsxfun:
m = rand(10,5);
v = rand(10,1);
vm = bsxfun(@times, v, m);
Here, ‘m’ is the matrix, ‘v’ is the vector, and ‘vm’ is the element-by-element product of the columns of ‘m’ and column vector ‘v’.

4 Comments

See if this works:
for k1 = 1:size(m,2)
vm(:,k1) = v .* m(:,k1);
end
If it doesn’t work for you, I’m certain this will:
for k1 = 1:size(m,2)
for k2 = 1:size(m,1)
vm(k2,k1) = v(k2) .* m(k2,k1);
end
end
You will get the same result with every version of my Answer.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!