Search code examples
matlabscalingcurve

Scale one dataset to another in Matlab


I have two datasets that they are a particular metric from two images (dat1 and dat2). I want both images to have the same response. The 'ideal' image should look like the first dataset (dat1)

enter image description here but the real image looks like the second dataset(dat2).

enter image description here

I want to try to 'fit' the second dataset to the first dataset. How can i scale dat2 so that it looks like dat1 using Matlab? I have tried to fit dat1 with different polynomials,exponentials or gaussians and then use the coefficients that i found to fit dat2 but the program fails and it does not fit properly, it gives me a straight zero line. When i try to fit dat2 using the same shape allowing the coefficients to be free then the program does not give me the ideal shape that i want because it follows the trends of dat2.

Is there any way to fit a dataset to a another set of data instead of a function?


Solution

  • Normally, in this situation, a very common approach consists in normalizing all the vectors between 0 and 1 (interval [0,1] with both extremes included). This can be easily achieved as follows:

    dat1_norm = rescale(dat1);
    dat2_norm = rescale(dat2);
    

    If you have a version of Matlab greater than or equal to 2017b, the rescale function is already included by default. Otherwise, it can be defined as follows:

    function x = rescale(x)
        x = x - min(x);
        x = x ./ max(x);
    end
    

    In order to achieve the objective you mention (rescaling dat1 based on minimum and maximum values of dat2), you can proceed as @cemsazara said in his comment:

    dat2_scaled = rescale(dat2,min(dat1),max(dat1));
    

    But this is a good solution only as long as you can identify the vector with the larger scale a priori. Otherwise, the risk is to rescale the smaller vector based on the values of the bigger one. That's why the first approach I suggested you may be a more comfortable solution.

    In order to adopt this second approach, if your Matlab version is less than 2017b, you must modify the custom rescale function defined above in order to accept two supplementar arguments:

    function x = rescale(x,mn,mx)
        if (nargin == 1)
            mn = min(x);
            mx = max(x);
        elseif ((nargin == 0) || (nargin == 2))
            error('Invalid number of arguments supplied.');
        end
    
        x = x - mn;
        x = x ./ mx;
    end