what is the simplest way to add two vectors if size is unknown?
1 回表示 (過去 30 日間)
古いコメントを表示
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(:);
0 件のコメント
回答 (3 件)
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(:);
0 件のコメント
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 件のコメント
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.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!