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 Ansicht (letzte 30 Tage)
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?

Akzeptierte Antwort

Jonas
Jonas am 13 Mai 2021
Bearbeitet: Jonas am 13 Mai 2021
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 Kommentare
Tanmoy Sarkar
Tanmoy Sarkar am 17 Mai 2021
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 am 17 Mai 2021
Bearbeitet: Jonas am 17 Mai 2021
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by