Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
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

Réponse acceptée

Walter Roberson
Walter Roberson le 3 Jan 2019
This is the same as sequence alignment, for which dynamic programming works well.
  2 commentaires
Aditya Tan
Aditya Tan le 5 Jan 2019
Hi Walter,
Thanks for your reply.
The biggest difference is that I don't know the "master chain," unlike in DNA sequences.
Or do we?
Aditya
Walter Roberson
Walter Roberson le 5 Jan 2019
At each point you compare current candidates . if they match exactly that is cost 0. otherwise you have either a gap in the top or a gap in the bottom of cost 1 and you do an appropriate shift. No master needed.

Connectez-vous pour commenter.

Plus de réponses (1)

Akira Agata
Akira Agata le 4 Jan 2019
If x1 contains all elements in x2 and x3, I believe the following code works.
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"];
% Prepare output array m1
m1 = [x1,repmat(missing,numel(x1),2)];
% Allocate x2's element to corresponding row of 2nd column
[~,loc] = ismember(x2,x1);
m1(loc,2) = x2;
% Allocate x3's element to corresponding row of 2nd column
[~,loc] = ismember(x3,x1);
m1(loc,3) = x3;
  1 commentaire
Aditya Tan
Aditya Tan le 10 Jan 2019
Hi Akira,
Thanks for your reply.
Your algorithm will work for this particular example, but unfortunately not all elements in x1 exist in x2 or x3.
Aditya

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by