Element-Wise Vector Division

조회 수: 347 (최근 30일)
Mark Rzewnicki
Mark Rzewnicki 2020년 1월 9일
댓글: Mark Rzewnicki 2020년 1월 9일
I would like to define a new vector based on element-wise scalar division with respect to an existing vector.
With scalar multiplication this works very naturally. For example, something like
vector = [1 x N row vector];
new_vector = K*vector;
automatically creates new_vector, which has the same dimensions as vector and whose elements are the corresponding elements of vector multiplied by K.
My goal is to do something very similar with scalar division. That is, I want to type something like
vector = [1 x N row vector];
new_vector = K/vector;
where new_vector has the same dimensions as vector and each element in new_vector is simply K divided by the corresponding element in vector.
(obviously the above code doesn't work - "matrix dimensions must agree")
I am able to obtain the vector I want manually with a for-loop:
vector = [1 x N row vector];
N = length(vector);
new_vector = zeros(1,N);
for i = 1:1:N
new_vector(i) = K/vector(i);
end
but I can't imagine that this is the most efficient way to perform this operation!
Can someone please advise?

채택된 답변

David Goodmanson
David Goodmanson 2020년 1월 9일
HI Mark,
new_vector = K./vector;
./ (dot divide) does element-by-element operations.
  댓글 수: 1
Mark Rzewnicki
Mark Rzewnicki 2020년 1월 9일
Ah, that'll do it. Thanks!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by