0

Essentially,I have 4 arrays in a data file. They are: theta (276 x 626 x 2677 double); dates (1 x 2677 date time); latitude (276 x 626 double); and longitude (276 x 626 double). I am basically trying to find d(theta)/dt.

I'm working in MATLAB and currently have:

data = load("data.mat");

[numLat, numLon, numDay] = size(data.theta);

%loop through the latitude and longitude 
for ilat = 1:numLat
    for ilong = 1:numLon
        X = squeeze(data.theta(ilat,ilong,:)); %[numDay x 1]
        ix = find(~isnan(X)); %finds the nonNaNs 
        XX = X(ix); %creates vector of nonNan thetas  
        dd = days(ix); %creates vector of nonNan days 
        XXdd = XX(2:end) - XX(1:end-1); %difference between adjacent thetas 
    end
end

However, this creates a 2677 x 1 vector X that is full of NaNs, and XX and dd are just [ ]. Am I doing something wrong??

Hannah
  • 11
  • 3
  • You described 4 arrays and then accessed `data.SM` that is not any of the arrays you described. And `X` comes from `data.SM`, but `days` is used before it exists. – Ben Voigt Sep 19 '22 at 21:20
  • what is `sum(sum(sum(isnan(data.SM))))` ? also `sum(sum(sum(isfinite(data.SM))))`? – Ben Voigt Sep 19 '22 at 21:23
  • 1
    @BenVoigt ```days``` is actually a MATLAB function, see https://www.mathworks.com/help/matlab/ref/duration.days.html. So, it is defined. – user16372530 Sep 19 '22 at 23:26
  • @Hannah: You are overwriting ```X```, ```XX```, and ```dd``` in each iteration. So, your results are produced in the last iteration for ```ilat``` and ```ilong``` equal to ```numLat``` and ```numLon```, respectively. My guess would be that ```data.SM(numLat,numLon,:)``` is simply all ```NaN```. – user16372530 Sep 19 '22 at 23:34
  • @BenVoigt Hi yes sorry, the data.SM should be data.theta and I have edited the question to reflect that. sum(sum(sum(insane(data.theta)))) = 332756015, and sum(sum(sum(isfinite(data.theta)))) = 129765337. – Hannah Sep 20 '22 at 02:24
  • @user16372530 so how do I prevent it from overwriting in each iteration? I want to add all the nonNaN values to this XX column vector – Hannah Sep 20 '22 at 02:26
  • “nonNAN” = “not not a number” = “a number”. Yet “not NAN” conveys different information than “number”. Weird! :) – Cris Luengo Sep 20 '22 at 03:16
  • @user16372530: Ok, but it's still an error in the code. The existing standard toolbox function is just concealing the failure to use OP's input data in that line. – Ben Voigt Sep 20 '22 at 15:00
  • With over 2/3rds of your data being NaN, it seems entirely possible that an entire slice of it contains no numbers, as @user16372530 surmised – Ben Voigt Sep 20 '22 at 15:02
  • I still don't understand what your final output should contain. For each combination of ```ilat``` and ```ilong```, the respective differences of ```data.theta(ilta,ilong,:)``` (without ```NaN```s)? If this is the case, you could store your results in a 2D cell-array of size ```numLat``` x ```numLon```. – user16372530 Sep 21 '22 at 04:13

0 Answers0