필터 지우기
필터 지우기

what is the simplest way to add two vectors if size is unknown?

조회 수: 2 (최근 30일)
Mr M.
Mr M. 2015년 9월 16일
댓글: Steven Lord 2015년 9월 16일
I would like to add v + u. I know v is 1XN but u can be 1xN or Nx1. Is it possible to do without if, and without defining a new variable, for example: w = u(:);

답변 (3개)

Thorsten
Thorsten 2015년 9월 16일
You can use the colon operator : to convert the 1XN or Nx1 vectors v and u to Nx1 and then add both
x = v(:) + u(:);

Titus Edelhofer
Titus Edelhofer 2015년 9월 16일
Hi,
if you know that v is a row:
x = v + u(:)';
Titus

Steven Lord
Steven Lord 2015년 9월 16일
v = 1:10;
u = v.^2;
if rand > 0.5
u = u.';
end
At this point, we can't be sure of the orientation of u. Let's force u to be the same orientation as v.
w = v + reshape(u, size(v));
  댓글 수: 2
Mr M.
Mr M. 2015년 9월 16일
sorry, i didnt describe the problem fully, in my case I would like to add: u(10:20) + v(30:40) and I dont know the size of v (coud be 1x10 or 10x1)
Steven Lord
Steven Lord 2015년 9월 16일
There's no such thing as the 30th element of a vector with 10 elements, so with either of your v vector orientations v(30) will error.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by