필터 지우기
필터 지우기

how to find the sum of 3 vectors of different sizes

조회 수: 1 (최근 30일)
aman ch
aman ch 2021년 6월 26일
편집: Jayant Gangwar 2021년 6월 28일
function [Z]=My_func(a,b,c);
v1=[9 0 -7 5 3 8 -10 4 2];
a=sort(v1)
v2=[3 6 -2 4 1 0];
b=sort(v2)
v3=[4 1 4];
c=sort(v3)
Z=sum(v1,v2,v3)
end
  댓글 수: 1
dpb
dpb 2021년 6월 26일
This is puzzling at best...you have three input arguments to the function but ignore those by defining local variables in their place -- whassup w/ that?
If the point is to sum() the three vectors, what's the point in sorting them first?
In fact, what is the point of writing a function here at all if the result wanted is just
v1=[9 0 -7 5 3 8 -10 4 2];
v2=[3 6 -2 4 1 0];
v3=[4 1 4];
Z=sum([v1,v2,v3]);

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

답변 (1개)

Jayant Gangwar
Jayant Gangwar 2021년 6월 28일
편집: Jayant Gangwar 2021년 6월 28일
It is my understanding that you have created 3 sorted vectors and you want to find the sum of all the elements of the 3 vectors and you are facing problems in using the inbuilt 'sum' function. It is because you are passing 3 vectors as input whereas 'sum' only takes a single vector or matrix as input along with a few optional inputs. Changing line 7 such that only a single vector is passed to the 'sum' function will achieve the required result
Z = sum([v1,v2,v3]);
Please refer to sum documentation for more information on how to find sum of arrays.

카테고리

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

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by