Hannah
Hannah

Reputation: 11

How to write a code that finds difference between adjacent, nonNaN values in a 3d array

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??

Upvotes: 0

Views: 38

Answers (0)

Related Questions