0
votes

I have many large arrays of temperature and pressure data, each contained in cells. I'd like to find the temperature values at each corresponding pressure index when the pressure value is closest to 850. Pressure is stored in [distance x height] and the resulting temperature arrays need to be [1 x distance] - as in, temperature needs to pull only one number from each pressure column.

I've tried finding the indices of each pressure array where the values are between 840 and 860:

for k = 1:length(hdfFiles)
  airTemp850{k} = airTemp{k}(pressure{k} > 840. & pressure{k} < 860.);
end

but sometimes there are no pressure values between these numbers. If I expand the range, it sometimes covers more than one pressure column and I end up with too many values in the temperature array.

I also tried finding the closest pressure values to 850 but this only finds the one value in the entire pressure array closest to 850.

for k = 1:length(hdfFiles)
 val = 850;
 diffVal{k} = abs(minus(pressure{k}(:),val));
 [idx idx] = min(diffVal{k}) % index of closest value to 850
 closest{k} = pressure{k}(idx{k}); 
end

What I'd like is for matlab to read through every row of pressure, find the value closest to 850, and pull out the corresponding temperature value. Any help is greatly appreciated. Thanks!

1

1 Answers

0
votes

I think min is the way to go. When called on matrices, it will operate along the required dimension. Your error comes from the use of (:) which vectorized the matrices. Plus you should use sub2ind to convert matrices index to linear index. Try this :

val = 850;
for k = 1:length(hdfFiles)
 diffVal = abs(pressure{k} - val); % no {k} if no storage need, no (:) and - seems a bit clearer than minus to me
 [~, idx] = min(diffVal, [], 2); % index of closest value to 850 along height dimension, ~ for unwanted output
 linind = sub2ind(size(pressure{k}), 1:size(pressure{k}, 1), idx);
 closest{k} = pressure{k}(linind); 
end