How to sum according to specific ranges within same table?
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello!
Available data: Please see the attached table.
Required: As shown in the same Excel file i would like to calculate the summ of test results for each person and based on four different intervals (1-10), (10-20), (20-30) and (30-40). then return a plot also for each person.
Any suggestions?
댓글 수: 0
채택된 답변
Pavan Guntha
2021년 4월 1일
Hi Hedi,
You could load the excel file as a table. You could refer to this documentation page link for more details. The following code illustrates the further steps to calculate sum based on a condition:
c = categories(TestConditionalSum.AvailableData);
resCat = zeros(length(c),4);
for i = 1:length(c)
t = TestConditionalSum(TestConditionalSum.AvailableData == c{i},:);
for j = 1:size(t,1)
if(t(j,:).From >= 30)
resCat(i,4) = resCat(i,4) + t(j,:).Res; % Res is the name of T-Results column in the table
elseif(t(j,:).From >= 20)
resCat(i,3) = resCat(i,3) + t(j,:).Res;
elseif(t(j,:).From >= 10)
resCat(i,2) = resCat(i,2) + t(j,:).Res;
elseif(t(j,:).From >= 1)
resCat(i,1) = resCat(i,1) + t(j,:).Res;
end
end
end
b = barh(resCat(4,end:-1:1)); % Example plot for datapoint 'Maya'
ylabel('Maya')
yticklabels({'4','3','2','1'})
The resCat matrix contains the required sum for the 4 respective intervals for each of the available datapoints. You could refer to the documentation of barh function for more details on plotting. The plot for the above code is as follows:
Hope this helps!
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Preprocessing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!