matlabsortingarduinocalibrationmpu6050

How to sort rows of a matrix based on the constraint of another matrix?


The 6 faces method is a very cheap and fast way to calibrate and accelerometer like my MPU6050, here a great description of the method.

I made 6 tests to calibrate the accelerometer based on the g vector.

enter image description here

After that I build up a matrix and in each row is stored the mean of each axis expressed in m/s^2, thanks to this question I automatically calculated the mean for each column in each file. enter image description here

The tests were randomly performed, I tested all the six positions, but I didn't follow any path. So I sorted manually the final matrix, based on the sort of the Y matrix, my reference matrix. The Y elements are fixed.

enter image description here

The matrix manually sorted is the following

enter image description here

Here how I manually sorted the matrix

    meanmatrix=[ax ay az];
    mean1=meanmatrix(1,:);
    mean2=meanmatrix(2,:);
    mean3=meanmatrix(3,:);
    mean4=meanmatrix(4,:);
    mean5=meanmatrix(5,:);
    mean6=meanmatrix(6,:);
    meanmatrix= [mean1; mean3; mean2; mean4;mean6;mean5];

Based on the Y matrix constrain how can sort my matrix without knowing "a priori" which is the test stored in the row?


Solution

  • Assuming that the bias on the accelerometer is not huge, you can look at the rows of your matrix and see with which of the rows in your Y matrix matches.

    sorted_meanmatrix = zeros(size(meanmatrix));
    for rows = 1:length(Y)
        % Calculates the square of distance and see which row has a nearest distcance 
        [~,index] = min(sum((meanmatrix - Y(rows,:)).^2, 2)); 
        sorted_meanmatrix(rows,:) = meanmatrix(index,:);
    end