How to do sum?

조회 수: 3 (최근 30일)
vimal kumar chawda
vimal kumar chawda 2020년 7월 16일
답변: Monisha Nalluru 2020년 8월 14일
I have data of 1891*4. In first row I have degree from 0 to 60 which has repetation of degree and I want to add all the same degree values which are in column 3 and add for column 4too. How can I do it?
if you load the data we are getting shc_06 and shc_12. Please find the attachment of code but I dont know how to calculate of same number degree in column1 have to added in column 3 and 4 respectively. So i will get 60*1 as output because the in column one we are having range from 0 to 60.
load('aiub_shc.mat')
k=1;
j=1;
del_cl_06 = shc06(:,3);
del_sl_06 = shc06(:,4);
del_cl_12 = shc12(:,3);
del_sl_12 = shc12(:,4);
for i=1:1:size(shc06)
if all(shc06(j,1))
std_cl_06(j,k)= del_cl_06(i,k)^2+del_sl_06(i,k)^2;
std_cl_12(j,k)= del_cl_12(i,k)^2+del_sl_12(i,k)^2;
j=j+1;
else
i=i+1;
end
end

답변 (1개)

Monisha Nalluru
Monisha Nalluru 2020년 8월 14일
From my understanding, you want to first group the data based on temperature and then add the values of values of 3rd and 4th column based on grouping.
In this case you may use findgroups in order to group the data and to perform operation on grouped data you can use splitapply
You may use below code as example
load('aiub_shc.mat')
G = findgroups(shc12(:,1));
col2sum=splitapply(@sum,shc12(:,2),G);
col3sum=splitapply(@sum,shc12(:,3),G);
col4sum=splitapply(@sum,shc12(:,4),G);
Hope this gives an idea!

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by