How to sum a portion of a row in a table and get a single value

4 ビュー (過去 30 日間)
Robert Demyanovich
Robert Demyanovich 2021 年 8 月 18 日
回答済み: Dave B 2021 年 8 月 18 日
How do I sum a portion of a row in a Matlab table and get a single value? For example assume that the row number is i and the columns to be summed are the columns (N+1)/2 to N. I can't seem to figure out how to use varfun to do this.

採用された回答

Dave B
Dave B 2021 年 8 月 18 日
To have sum apply to all elements of the array rather than across a particular dimension, you can use the 'all' flag. (Alternates that are common in old code: sum(sum(X)) or sum(X(:)) also work for a matrix X).
a=rand(10);
i = 4;
N = 5;
cols = (N+1)/2:N;
sum(a(i,cols),'all')
ans = 1.2780

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by