MATLAB: How to subtract two data sets with different resolutions in MATLAB

datasetsmappingMATLABresolutionsubtraction

I have two data sets with different resolutions and wish to subtract one from the other.

Best Answer

In order to subtract two data sets with different resolutions, it is required to convert the resolution of one of the data sets to match that of the other one. Use MATLAB function GRIDDATA for this purpose.
Following is an example for subtracting two data sets with different resolutions:
%%Let's create dataset A: [XX YY ZZ]
x = 1:10
y = 1:10
z = rand(10)
[XX YY] = meshgrid(x,y);
% meshgrid(x,y) transforms the domain specified by vectors x and y into arrays X and Y, which can be used to evaluate functions of two variables
ZZ = z;
surf(XX,YY,ZZ); % To plot the surface of the specified data set 'A'
%%Let's create dataset B: [XX2 YY2 ZZ2]
x = 1:0.4:10;
y = 1:0.4:10;
[XX2 YY2] = meshgrid(x,y)
ZZ2 = rand(size(XX2))
figure
surf(XX2,YY2,ZZ2) % To plot the surface of the specified data set 'B'
%%Note that the two data sets A and B have different sizes
size(XX) %Gives the size of the data set A
size(XX2)%Gives the size of the data set B
ZZ3 = griddata(XX,YY,ZZ,XX2,YY2);
% griddata(XX,YY,ZZ,XX2,YY2) fits a surface of the form ZZ = f(XX,YY) to the data in the nonuniformly spaced vectors (XX,YY,ZZ). griddata interpolates this surface at the points specified by (XX2,YY2) to produce ZZ3.
size(ZZ3) % This will give the changed size/resolution of the data set 'A' which would now be same as that of the data set 'B'.
%%Plots
subplot(1,2,1)
surf(XX,YY,ZZ) % Plot of the original data set 'A'
subplot(1,2,2)
surf(XX2,YY2,ZZ3) % Plot of the new data set 'A' which has the same size/resolution as that of the data set B.
% We can now subtract data sets A and B.
ZZ2-ZZ3