nX2 matrix. Random instances of 0,1,2... in column 1. Need to add all corresponding values of column 2 for each of 0,1,2... and create a matrix m X 2 where 1st column is 0,1,2.. and second column is the resultant sum of previous defined content.

1 回表示 (過去 30 日間)
Hi all,
I am a beginner in Matlab and most of my learnings come from kind answers to different problems in these sections.
I have a 11300 X 2 matrix which I imported from an excel file. Column 1 has 90 intsnaces of 0, 75 instances of 1, 87 instances of 2 and so on. So the number of instancs of 1,2,3….190 is random. For example:
0 0.16
0 0.23
. .
. .
1 9.34
1 0.34
. .
. .
190 4.5
Each instances has certain corresponding values in the column. I am looking to add up all the values of corresponding value of column 2 so that I can have the 191 X2 matrix looking as following:
0 2.7 % (ex:2.7 = 0.16 + 0.23 +.. all other values in second column corresponding to 0)%from above example
1 15.5 % (ex:15.5 = 9.34 + 0.34 +.. all other values in second column corresponding to 1)%from above example
2 0.6 % and so on as above
. .
190 9.8
Is there anyway I can get some help?

採用された回答

Jonas
Jonas 2021 年 5 月 13 日
編集済み: Jonas 2021 年 5 月 13 日
dirty solution with for:
tbl=zeros(191,2);
for number=1:191
tbl(number,1)=number-1;
tbl(number,2)=sum(data(data(:,1)==number-1,2));
end
  2 件のコメント
Tanmoy Sarkar
Tanmoy Sarkar 2021 年 5 月 17 日
Thanks a lot. Exactly what I was looking for. For my learning, if you can please elaborate what the following line does in the loop,
Thanks.
tbl(number,2)=sum(data(data(:,1)==number-1,2))
Jonas
Jonas 2021 年 5 月 17 日
編集済み: Jonas 2021 年 5 月 17 日
data(:,1)==number-1
get logical index for the rows which contain the current number we are interested in
data(..,2)
get values from second column at the rows we specified in the mentioned positions
sum(..)
sum those values

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by