How to multiple a given element of a column vector across the elements of a row in a matrix, for each corresponding row.

I wish to multiple the elements of a row vector across the rows of the corresponding row of a matrix. Here's an example:
X = [1 2 3]' and y = [1 1 1; 1 1 1; 1 1 1] I want the first element of X to multiply across the first row of y, the 2nd element of x to multiply across the 2nd row of y, and so on, which should give:
output = [1 1 1; 2 2 2; 3 3 3].
I can do this with a complicated for-loop, but surely MATLAB has some elegant and compact way to handle this, especially for very large matrices and of arbitrary dimension, which is what I ultimately want to do.
Thanks for the help.

 채택된 답변

X = [1 2 3]'
y = [1 1 1; 1 1 1; 1 1 1]
out=bsxfun(@times,y,X)

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2015년 8월 15일

댓글:

2015년 8월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by