Determine identical value of a column

1 回表示 (過去 30 日間)
Kim Lopez
Kim Lopez 2017 年 10 月 18 日
コメント済み: KL 2017 年 10 月 18 日
Supposed i have this table, values in row 2 and row 3 in column1 are identical, same also at row 6 and row 7. Is there a way I can determine those values and add and store the values and then remove one identical value?
11 0.9
8 0.3
8 0.7
6 0.2
5 2.3
3 4.6
3 6.1
0 8.7
the output table would look like this
11 0.9
8 0.10
6 0.2
5 2.3
3 10.7
0 8.7
  1 件のコメント
Stephen23
Stephen23 2017 年 10 月 18 日
Is this really the output that you want?:
8 0.10

サインインしてコメントする。

採用された回答

KL
KL 2017 年 10 月 18 日
編集済み: KL 2017 年 10 月 18 日
You could use findgroups and splitapply
data = [11 0.9
8 0.3
8 0.7
6 0.2
5 2.3
3 4.6
3 6.1
0 8.7];
grps = findgroups(data(:,1)); %find groups here
grp_sum = splitapply(@sum,data(:,2),grps); %apply sum on these groups
res = [unique(data(:,1)) grp_sum] %summarize the result
note that the resultant array is sorted and also you'd need 2015b or later.
For older versions, use a table maybe,
t = array2table(data);
g = varfun(@sum, t, 'GroupingVariable','data1')
  4 件のコメント
Kim Lopez
Kim Lopez 2017 年 10 月 18 日
By the way, is this also working on cells?
KL
KL 2017 年 10 月 18 日
Probably you will have to superimpose it with a cellfun in that case. I'd prefer a table for such data manipulation.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by