Compare elements and shift the elements up or down based on matching previous or next elements
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Walter Roberson
2019-1-3
This is the same as sequence alignment, for which dynamic programming works well.
2 个评论
Walter Roberson
2019-1-5
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.
更多回答(1 个)
Akira Agata
2019-1-4
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;
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 PID Controller Tuning 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!