MATLAB: Compare elements and shift the elements up or down based on matching previous or next elements

comparematchingshifting

Hi,
A few days ago I have tried to simplify my problem by asking a related question here
but it seems that my problem is much bigger than the simplified problem. Thus please allow me to explain this issue again.
So suppose I have the following three arrays, and let me stress that they may or may not have the same length. In this example, x2 has two less entries than the rest.
x1 = ...
[ "Liability and Equity";
"Short-term Liability";
"Long-term Liability";
"Equity";
"Attributable Equity";
"Non-attributable Equity";
"Total Liability and Equity"];
x2 = ...
[ "Liability and Equity";
"Equity";
"Attributable Equity";
"Non-attributable Equity";
"Total Liability and Equity"];
x3 = ...
[ "Liability and Equity";
"Short-term Liability";
"Long-term Liability";
"Equity";
"Attributable Equity";
"Non-attributable Equity";
"Total Liability and Equity"];
So if I add an entry to x2 at the end (to make them have the same length) and put them into a matrix, this is what I have (it's easier to explain in this matrix form)
m1 = [x1 x2 x3];
% "Liability and Equity" "Liability and Equity" "Liability and Equity"

% "Short-term Liability" "Equity" "Short-term Liability"
% "Long-term Liability" "Attributable Equity" "Long-term Liability"
% "Equity" "Non-attributable Equity" "Equity"
% "Attributable Equity" "Total Liability and Equity" "Attributable Equity"
% "Non-attributable Equity" "<missing>" "Non-attributable Equity"
% "Total Liability and Equity" "<missing>" "Total Liability and Equity"
And my goal is to turn my matrix m1 into the following:
% "Liability and Equity" "Liability and Equity" "Liability and Equity"
% "Short-term Liability" "<missing>" "Short-term Liability"
% "Long-term Liability" "<missing>" "Long-term Liability"
% "Equity" "Equity" "Equity"
% "Attributable Equity" "Attributable Equity" "Attributable Equity"
% "Non-attributable Equity" "Non-attributable Equity" "Non-attributable Equity"
% "Total Liability and Equity" "Total Liability and Equity" "Total Liability and Equity"
So, specifically, I would like to know how to compare the entries, and then shift them up or down based on the "matching algorithim".
Thanks much for your help.
Aditya

Best Answer

This is the same as sequence alignment, for which dynamic programming works well.
Related Question