Search code examples
arraysmatlabvectorarray-difference

Matlab: Difference bettween two different sized arrays


Is it possible to find the difference beetwen two arrays of different size?

My problem is that I have two arrays, that scaled are pretty similar and I need the error in each point.

The data look like this:-

enter image description here

Yaw data is much bigger than Yaw Ref.


Solution

  • You could take a very naive approach and simply pad each element of the reference array. That is fairly simple to do:

    n = length(yaw)/length(yaw_ref);
    
    yaw_ref_pad = zeros(length(yaw), 1);
    for j = 1:length(yaw_ref)-1
        yaw_ref_pad((n*j):(n*(j+1)) = yaw_ref(j);
    end
    

    You could also do something more adaptive, which may or may not be what you want. This approach uses the derivatives to determine where the padded reference should switch. This might be considered a bit circular, since your system looks like an overdamped PID system and this uses the output to seed the input.

    yaw_ref_pad = zeros(length(yaw), 1);
    [x, peaks] = findpeaks(diff(yaw));
    for j = 1:length(peaks)-1
        yaw_ref_pad(peaks(j):peaks(j+1)) = yaw_ref(j);
    end
    

    Either way, after filling yaw_ref_pad, your result is simply

    error = yaw_ref_pad - yaw;