problem with coding cumsum statement

조회 수: 2 (최근 30일)
D.J
D.J 2018년 8월 31일
댓글: D.J 2018년 8월 31일
Hi all, I am really stuck with this one ! I am trying to tell matlab to calculate at which day the cumulative rainfall reaches 8mm I should only use vectorisation for this code. 2 issues here:
1- I keep getting this error: "CUMSUM supports only numerical or logical inputs." If I create the arrays manually cumsum seems to work, but if I call it from a table, it give me the above error. Does this mean, I need to create the arrays manually?
2- If if I manage to get it to work, how on Earth do I tell matlab to keep adding the rainfall values until it reaches 8mm then exit and display on which day the goal was reached without using loop ?? I have attached the file and a draft of my code below: Any help would be highly appreciated !
Many thanks in advance !
RF_tbl=readtable('Test_data1.csv');
date=[1:31]; % I tried first to call the date column from the table but that caused a problem.
RF_V= RF_tbl(6:36,5); %This calls the rainfall column from the table, which I need to calculate
if cumsum(RF_V)<=8
'continue'
else
'exit and display the date on which the goal reached' % This is not the code, this is just to explain what I need to achieve.
end

채택된 답변

Dennis
Dennis 2018년 8월 31일
Your data are stored as cells, not as matrix. Your values are also strings and need to be cast to numbers first. This code might work for you (it will throw an error if the 8mm rainfall are not reached within the month):
data=readtable('Test_data1.csv');
mysum=cumsum(str2double(data{6:end,5}));
idx=find(mysum>=8,1);
date=data{idx+6,2}
  댓글 수: 1
D.J
D.J 2018년 8월 31일
Thanks a million Dennis. That solved the problem, and thanks heaps for explaining where did I go wrong ! Much appreciated.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Paolo
Paolo 2018년 8월 31일
편집: Paolo 2018년 8월 31일
A few things here. First of all, you can't apply cumsum because, as MATLAB is telling you, the data is not numeric. When you are reading data from the table, it is converted to char data type. You can either specify the type of variable when using readtable, or convert it afterwards. In my solution below, I convert it afterwards to show you one way of doing it.
I also think that what you do not want to use cumsum here, but rather a simple sum in a while loop until your exit condition is met:
RF_tbl=readtable('Test_data1.csv');
date=[1:31]; % I tried first to call the date column from the table but that caused a problem.
RF_V= str2double(RF_tbl(6:36,5).Var5);
indx=1;
tmpsum=0;
while(tmpsum<=8)
tmpsum=tmpsum+RF_V(indx);
indx=indx+1;
end
fprintf('Reached on day:%d\n',indx-1)
Prints:
Reached on day:8
  댓글 수: 1
D.J
D.J 2018년 8월 31일
Many thanks Paolo, but I am not supposed to use while or for loops. Problem has been solved in another code. All good now. Many thanks!

댓글을 달려면 로그인하십시오.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by