Extract data from dataset with no nans and loop over columns

조회 수: 2 (최근 30일)
J A
J A 2021년 11월 28일
댓글: J A 2021년 12월 1일
Hi Matlab community,
I have run into a problem in my code and would very much appreciate your help.
I have three matrices of size 1825*52 with annual data. I want to extract the months January to june from the dataset when none of these three matrices have nan values and save it in three new matrices. I am able to make it work for one column, but then the code fails to loop for 52 columns. Rows are annual data from 2015 to 2019(no 31st dec data, so it has 1825 values) and columns are 52 different simulations.
Here is my code:
obs=readmatrix('cms.xlsx', 'Range','B367:BA2191');
sim1=readmatrix('avg.csv', 'Range','B367:BA2191');
sim2=readmatrix('scaled.csv','Range','B367:BA2191');
%Read only january to June with non nans for all years and all columns
startdate='2015-01-01';
enddate='2019-12-30';
t1=datetime(startdate,'InputFormat','yyyy-MM-dd');
t2=datetime(enddate,'InputFormat','yyyy-MM-dd');
time=t1:t2;
time=time';
m=0;
for n=1:52
for i=1:length(obs) %2015 to 2019 there is no 31st dec 2019 data so 1 value less
if month(time(i))>=1 && month(time(i))<=6 && isnan(obs(i,n))==0 && isnan(sim1(i,n))==0 && isnan(sim2(i,n))==0
m=m+1;
obsnew(m,n)=obs(m,n);
sim1_new(m,n)=sim1(m,n)
sim2_new(m,n)=sim2(m,n);
end
end
%n=n+1;
end
Thanks in advance for your suggestions and help!

채택된 답변

Voss
Voss 2021년 11월 30일
I think, at least, these three lines:
obsnew(m,n)=obs(m,n);
sim1_new(m,n)=sim1(m,n);
sim2_new(m,n)=sim2(m,n);
should be modified to this:
obsnew(m,n)=obs(i,n);
sim1_new(m,n)=sim1(i,n);
sim2_new(m,n)=sim2(i,n);
since i is the row index you're checking and m is the row index of the good values only.
I'm not sure if this change is sufficient to make it work.
  댓글 수: 5
Voss
Voss 2021년 12월 1일
Awesome! Do you mind marking my answer as Accepted (so that the question won't show up as unanswered anymore - and so that I get credit)? Thanks
J A
J A 2021년 12월 1일
Sure, I did it. Thanks

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by