필터 지우기
필터 지우기

How to multiply a matrix by certain numbers?

조회 수: 1 (최근 30일)
Royvg94
Royvg94 2015년 9월 23일
편집: Stephen23 2019년 6월 30일
I want to multiply a matrix by a column vector in this way:
(4 2 3 8;7 9 1 5;6 4 8 3) * (4;8;2)
and then the result i want to get is:
(4*4 2*4 3*4 8*4;7*8 9*8 1*8 5*8;6*2 4*2 8*2 3*2)
  댓글 수: 3
madhan ravi
madhan ravi 2019년 6월 30일
xy = repmat(x,1,1,numel(x)) .* reshape(y,1,1,[]); % for version <=2016b bsxfun(@times,repmat(x,1,1,numel(x)) , reshape(y,1,1,[]))
Wanted = reshape(squeeze(xy).',1,[])
Stephen23
Stephen23 2019년 6월 30일
편집: Stephen23 2019년 6월 30일
@Shubha Baravani: the simple MATLAB way:
>> x = [1,2,3,4];
>> y = [2,3,4,5];
>> z = kron(x,y)
z =
2 3 4 5 4 6 8 10 6 9 12 15 8 12 16 20

댓글을 달려면 로그인하십시오.

채택된 답변

Stalin Samuel
Stalin Samuel 2015년 9월 23일
A = [4 2 3 8;7 9 1 5;6 4 8 3];
B = [4;8;2]
for i = 1:length(B)
C(i,:) = A(i,:)*B(i)
end
  댓글 수: 1
Stephen23
Stephen23 2015년 9월 23일
편집: Stephen23 2019년 6월 30일
Note that:
  1. The array C expands on every iteration, which is slow and inefficient. Preallcoation would resolve this.
  2. Using a loop is more complex than using vectorized code.
  3. The variable name i should be avoided, as it is the name of the imaginary unit.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
Stephen23 2015년 9월 23일
편집: Stephen23 2015년 9월 23일
Just use bsxfun:
>> X = [4 2 3 8;7 9 1 5;6 4 8 3];
>> Y = [4;8;2];
>> bsxfun(@times,X,Y)
ans =
16 8 12 32
56 72 8 40
12 8 16 6
  댓글 수: 1
Royvg94
Royvg94 2015년 9월 23일
This one is much better, thanks! But i cant change my accepted answer right?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by