how to sum a result with multiple values

조회 수: 9 (최근 30일)
Opariuc Andrei
Opariuc Andrei 2021년 4월 13일
댓글: Opariuc Andrei 2021년 4월 13일
a=[170;115;110;70;80;100;140;65;80;99;48];
b=[40;20;27;20;18;20;30;14;13;22;17];
k=a./(a-b)
k results in 11 values as intended, i'd like to do Kmed=k/11 how can i add the 11 values of k=a./(a-b) into 1 value without having to manually insert all 11 values and then divide by 11 ?

채택된 답변

madhan ravi
madhan ravi 2021년 4월 13일
편집: madhan ravi 2021년 4월 13일
You literally answered your own question, just use sum()
help sum
SUM Sum of elements. S = SUM(X) is the sum of the elements of the vector X. If X is a matrix, S is a row vector with the sum over each column. For N-D arrays, SUM(X) operates along the first non-singleton dimension. S = SUM(X,'all') sums all elements of X. S = SUM(X,DIM) sums along the dimension DIM. S = SUM(X,VECDIM) operates on the dimensions specified in the vector VECDIM. For example, SUM(X,[1 2]) operates on the elements contained in the first and second dimensions of X. S = SUM(...,OUTTYPE) specifies the type in which the sum is performed, and the type of S. Available options are: 'double' - S has class double for any input X 'native' - S has the same class as X 'default' - If X is floating point, that is double or single, S has the same class as X. If X is not floating point, S has class double. S = SUM(...,NANFLAG) specifies how NaN (Not-A-Number) values are treated. The default is 'includenan': 'includenan' - the sum of a vector containing NaN values is also NaN. 'omitnan' - the sum of a vector containing NaN values is the sum of all its non-NaN elements. If all elements are NaN, the result is 0. Examples: X = [0 1 2; 3 4 5] sum(X, 1) sum(X, 2) X = int8(1:20) sum(X) % returns double(210), accumulates in double sum(X,'native') % returns int8(127), because it accumulates in % int8 but overflows and saturates. See also PROD, CUMSUM, DIFF, ACCUMARRAY, ISFLOAT. Documentation for sum doc sum Other functions named sum codistributed/sum duration/sum sym/sum timeseries/sum dlarray/sum gpuArray/sum tall/sum

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by