Sum question in Matlab
49 次查看(过去 30 天)
显示 更早的评论
i'm confusing of how to sum two different vector in matlab
Scuh as v1=[1 2]
v2= [3 4]
and i wanna got the output of 10 which mean sum those number together
function [Vectors] = sumVectors(v1, v2)
Vectors = v1+v2;
end
this is the code i have done so far
0 个评论
采纳的回答
BALAJI KARTHEEK
2020-4-23
Hii buddy,
There is something you want to know in matlab..
if u sum two vectors, it eventually results in to vector of individual sum of the elements. In your case v1+v2 results into a vector of [4 6], and if u want to sum all the elements in a vector use sum function. for example sum(v1) gives you the scalar value of 3 sum(v2) gives you the scalar value of 7. So you want to modify your code as Vectors=sum(v1)+sum(v2).
更多回答(1 个)
KSSV
2020-4-23
v1 = [1 2] ;
v2 = [3 4] ;
% MEthod 1
v = [v1 v2] ;
iwant = sum(v) ;
% method 2
v = v1+v2 ;
iwant = sum(v) ;
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!