필터 지우기
필터 지우기

Getting a j x k Matrix from j and k-dimensional Vectors Without a For Loop?

조회 수: 1 (최근 30일)
Suppose I have two column vectors:
J = j x 1 vector
K = k x 1 vector
And I want to create a j x k matrix, W:
W = j x k matrix
where the columns of W are formed by multiplying the corresponding element of K by the vector J.
The for loop to create W looks like this :
% J = j x 1 vector
% K = k x 1 vector
W = zeros(length(J),length(K));
for i = 1:1:length(K)
W(:,i) = K(i)*J;
end
Numerical example:
J = [1 2 3]';
K = [4 5]';
should result in
W =
4 5
8 10
12 15
Is there any way I can avoid the for loop here? Thanks!
  댓글 수: 2
Mark Rzewnicki
Mark Rzewnicki 2020년 3월 23일
Nope, just a project for fun on the side. I'm looking at large computing times here so I'm anxious to avoid as many loops as possible.

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

채택된 답변

per isakson
per isakson 2020년 3월 23일
편집: per isakson 2020년 3월 24일
>> J*K'
ans =
4 5
8 10
12 15
  댓글 수: 1
Mark Rzewnicki
Mark Rzewnicki 2020년 3월 23일
Hah, now I see why you asked if it was homework. Should have been obvious that all I needed to do was transpose.
Thank you!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by