Interpolation by pressure using interp1
2 views (last 30 days)
Show older comments
Hello all,
I have a table with 3 variables: sampling station, pressure (decibar) and temperature. For each of my 46 stations, I have the temperature by pressure. Like so:
Station Pressure Temperature
B 0.25 1.2
B 0.33 1.21
B 0.77 1.22
...
C 0.12 ...
Now, I simply want to interpolate the data (pressure and temperature) per 1 decibar for each station so I have the following result:
Station Pressure Temperature
B 1 1.5
B 2 1.54
B 3 1.59
...
C 1 ...
Is there an easy way to do this using interp1?
2 Comments
KSSV
on 2 Feb 2017
You may read this documentation: https://in.mathworks.com/help/matlab/ref/interp1.html. We comment on your data if we have data in hand.
Jan
on 2 Feb 2017
The relation between "B 0.25 1.2" and "B 1 1.5" is not clear. Please post an example for the real data in valid Matlab syntax. Currently the data look like a text file. But for suggestion a solution, using variables would be more useful.
Accepted Answer
dpb
on 2 Feb 2017
Edited: dpb
on 2 Feb 2017
Rough outline to get you started...
>> S=categorical(cellstr([repmat('B',3,1); repmat('C',5,1)])) % made up a few extras...
S =
B
B
B
C
C
C
C
C
T=[1.2:0.01:1.22 linspace(1.4, 0.023,5)].'; % ditto for other variables
P=[0.25 .33 .77 linspace(0.12,0.8,5)].';
C=categories(S); % the list of extant categories (stations)
for i=1:length(C) % iterate over that list
ix=S==C{i}; % select each category in order (logical addressing vector)
interp1(P(ix),T(ix),[0.2:0.1:1].') % interpolate over those values for a given range
end
ans =
NaN
1.2063
1.2116
1.2139
1.2161
1.2184
NaN
NaN
NaN
ans =
1.2380
1.0355
0.8330
0.6305
0.4280
0.2255
0.0230
NaN
NaN
>>
Note above there are a bunch of NaN's 'cuz your range of inputs don't span the range asked for in the interpolation and didn't set the 'extrapolate' parameter.
But, when you get the full data set and make the interpolating points vector match the desired, you'll get output.
Also, I just dumped result to command window; you'll have to save the results in another variable or cell array or however you wish to subsequently use the results.
But, that's the idea. Matlab needs more sophisticated ways of using categorical variables than it has; there are ways to write such things via accumarray and all, but is very convoluted logic as compared to, say, SAS BY constructs.
3 Comments
dpb
on 3 Feb 2017
Edited: dpb
on 3 Feb 2017
Or, presuming the input vector for the interpolating points is the same for each station you can just use a 2D array...
Before the loop begins insert
result=zeros(length(WantedP),length(C));
then
result(:,i)=interp1(...
in the loop to populate the array.
That there are multiple ways possible and which is best depends on how intend to use the results is why left as "exercise for the student"... :)
More Answers (0)
See Also
Categories
Find more on Live Scripts and Functions in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!