Sum question in Matlab

조회 수: 49 (최근 30일)
ka kei yeung
ka kei yeung 2020년 4월 23일
댓글: ka kei yeung 2020년 4월 23일
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

채택된 답변

BALAJI KARTHEEK
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
ka kei yeung
ka kei yeung 2020년 4월 23일
Oh my goddddd, that's the things I'm confusing. oh my god, thx for helping me figure it out.
cheers.

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

추가 답변 (1개)

KSSV
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) ;
  댓글 수: 1
ka kei yeung
ka kei yeung 2020년 4월 23일
i have tried that, seems like it's going to be work
but I got the value output of 1 2 3 4 when i'm using mt driver code to test the code
function [Vectors] = sumVectors(v1,v2)
Vectors = [v1 v2];
iwant = sum(Vectors);
end
and here my driver code
v1 = [1 2];
v2 = [3 4];
sum = sumVectors(v1,v2);
disp(sum)

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by