How to vectorize the following code?

조회 수: 1 (최근 30일)
Jordan Tryggvesson
Jordan Tryggvesson 2021년 5월 10일
댓글: Jordan Tryggvesson 2021년 5월 10일
I have an nx1 array f and a 3xm array g. I want to carry out the following insertion, where I pick index pairs from the first two rows of g and values from the last row of g:
for int i = 1:m
f(g(1:2,i)) = g(3,i);
end
How can I vectorize the above code? The simple
f(g(1:2,:)) = g(3,:)
does not work and returns the following error
Unable to perform assignment because the left and right sides have a different
number of elements.
Any suggestions?

채택된 답변

Walter Roberson
Walter Roberson 2021년 5월 10일
f(g(1:2,:)) = g([3 3], :);
  댓글 수: 3
Walter Roberson
Walter Roberson 2021년 5월 10일
Looks okay. Another way would be
f(g(1:2,:)) = kron([1;1], g(3,:).*g(4,:));
Jordan Tryggvesson
Jordan Tryggvesson 2021년 5월 10일
Alright, thanks for the help!

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

추가 답변 (0개)

카테고리

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