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!