how to put an if statement within for loop
显示 更早的评论
I compile climate data over many years by month within a for loop, and concatenate these to create a 3D array (of say January's over many years), and I want to take the element-wise mean to get the average over those years:
monthlydata = cell(1, numel(years)); %to hold the monthly data of each year. Preallocated.
years = 2003:2014;
for month = 1:12
for y = 1:numel(years)
monthlydata{y} = load(sprintf('sst_%d_%d.asc', years(y), month));
end
monthlydatac = cat(3, monthlydata{:}); %concatenate all cells along 3rd dimension
monthlymean = mean(monthlydatac, 3); %get mean along 3 dimensions
dlmwrite(sprintf('msst_%d.csv', month), monthlymean); %save
end
The problem is there is missing data for some months taking the form of numbers 250, 251, 252, 253, 254, 255. In some years observations occur in elements of the 3D array but in other years data is missing ( coded as 250:255) for that element. If this is the case I want to take the element-wise mean excluding these 'missing data' values. Would an 'if' statement work? Could anyone advise me on how to do this?
I am new to Matlab and programming and would be very greatly for your help.
4 个评论
Jan
2017-10-4
I do not understand "missing data in some elements of some months in the form of 255, 254, 253, 252, 251 or 250". What is missing? The file or the data? What does "In some years real observations occur where usually it they are missing" mean? What does it mean, that you want to "specify" this? What is "take whatever observation is real" exactly?
Please edit the question and define exactly, what you want to do with which values. I'm sure it will work e.g. with an if command. But for an explicit suggestion, it is required to know, what you want.
Roisin Loughnane
2017-10-4
编辑:Roisin Loughnane
2017-10-4
Jan
2017-10-4
No, this does not help. What is "coded as one of those numbers 250:255"? How could we suggest an explicit solution based on the detail, that "some" elements of "some" years "may" have a value, that you "would like" to keep?
An exact, unequivocal, clear and unique definition is required, to implement this. We cannot do this for you. I assume the actual implementation will be very easy then.
Roisin Loughnane
2017-10-4
编辑:Roisin Loughnane
2017-10-4
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!