Interpolating by latitude and depth
    8 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Lavenia
 el 3 de Feb. de 2024
  
    
    
    
    
    Comentada: Walter Roberson
      
      
 el 4 de Feb. de 2024
            I am very new to Matlab - coming from an R background and i could use some help getting started. 
We sampled at numerous 'stations' along a latitudinal gradient. At each station, we collected samples from 10-20 depths. So i have a dataset that has latitude, longitude, station number, depth, and a whole range of variables (nutrients etc.). 
I need to do a 2D interpolation of the data. 1) To interpolate the nutrient data from the surface to 4000 m at 1m intervals and also 2) across the latitude -60 to -58 degrees. What i have done is:
data = readtable('mock_data.csv')
[val, q] = unique(data.nut1);
z_profile = [1:4000]; 
Xq = -60:0.01:-58;
if length(q) > 0
   Vq = interp2(data.latitude(q), data.depth(q), data.nut1(q), Xq, z_profile); 
end
But i get an error 
Error using griddedInterpolant
Interpolation requires at least two sample points for each grid dimension.
Error in interp2>makegriddedinterp (line 226)
    F = griddedInterpolant(varargin{:});
Error in interp2 (line 126)
        F = makegriddedinterp({X, Y}, V, method,extrap);
I used the example from herehttps://au.mathworks.com/help/matlab/ref/interp2.html Vq = interp2(X,Y,V,Xq,Yq) but im wondering if this error is because i have a number of "stations' that much each be treated independently? 
Ive added a mock dataset, if anyone could point me in the right direction that would be greatly appreciated! 
0 comentarios
Respuesta aceptada
  Star Strider
      
      
 el 3 de Feb. de 2024
        Try this — 
T1 = readtable('mock_data.csv')
FNut1 = scatteredInterpolant(T1.Lat, T1.depth, T1.Nut1);
Xq = -60:0.01:-58;                                          % Latitude Vector
Yq = linspace(0, 150, numel(Xq));                           % Depth Vector
[Lat_mtx,Dpt_mtx] = ndgrid(Xq, Yq);
Nut1_mtx = FNut1(Lat_mtx, Dpt_mtx);
figure
surfc(Lat_mtx, Dpt_mtx, Nut1_mtx)
grid on
colormap(turbo)
xlabel('Latitude (°)')
ylabel('Depth (m)')
zlabel('Nutrient 1 (mol/L)')
FNut1 = scatteredInterpolant(T1.Lat, T1.depth, T1.Nut2);
Xq = -60:0.01:-58;                                          % Latitude Vector
Yq = linspace(0, 150, numel(Xq));                           % Depth Vector
[Lat_mtx,Dpt_mtx] = ndgrid(Xq, Yq);
Nut1_mtx = FNut1(Lat_mtx, Dpt_mtx);
figure
surfc(Lat_mtx, Dpt_mtx, Nut1_mtx)
grid on
colormap(turbo)
xlabel('Latitude (°)')
ylabel('Depth (m)')
zlabel('Nutrient 2 (mol/L)')
You will have to repeat this for each nutirent (or other variable), however that is not difficult.  
.
16 comentarios
  Star Strider
      
      
 el 4 de Feb. de 2024
				I am just guessing since I don’t have your data, however starting ‘Yq’ from 0 instead of 1 may solve that — 
Yq = 0:1:4000;
instead of: 
Yq = [1:1:4000];
Also, the square brackets are not necessary and can slow your code.  
Más respuestas (1)
  Walter Roberson
      
      
 el 3 de Feb. de 2024
        interp2() expects the first two inputs to be either vectors or 2d arrays. If they are vectors then interp2() automatically expands them into grids.
interp2() expects the third input to be a 2D array.
It is not clear what q is in your code. If it is a scalar, then you would be asking to interp2() a scalar, a scalar, and a scalar -- not something that is suitable for interpolating over.
If your q is a vector of indices or is a logical mask, then latitude(q) and depth(q) are potentially vectors, so that part potentially works. But nut(q) would have to be a vector in that circumstance, not a 2d array.
The first two parameters are effectively there to describe the "marginal" indices of the grid of data that is the third parameter.
2 comentarios
  Walter Roberson
      
      
 el 4 de Feb. de 2024
				The first two inputs (latitude and depth) are vectors. The third input is the nutrient concentration which varies with depth and by latitude. 
For interp2() purposes, the third input must be length(depth) by length(latitude)
%example
latitude = linspace(0,1,10);
depth = linspace(0,1,15);
nc = rand(length(depth), length(latitude));
interp2(latitude, depth, nc, [0.15], [0.73])
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!











