Sorting a string by the use of another predetermined string order
6 次查看(过去 30 天)
显示 更早的评论
So I have theses 2 arrays below where array A is the desired order and I would like to have array B placed in the same order. I have tried FEX nat_sort, ismember, and other pattern sorting techniques that I have come up with on my own but to no avail I have figured out how to sort these the same.
A = "FLOATING.NET.P2" "PDSTL" "POLY1.1" "POLY1.5" "CONT.1" "ANCH.4" "MET.3" "BEAMS.3" "GERING.1" "GERING.2" "CAVE.3";
B = "ANCH.4" "BEAMS.3" "CAVE.3" "CONT.1" "FLOATING.NET.P2" "GERING.1" "GERING.2" "MET.3" "PDSTL" "POLY1.1" "POLY1.5";
4 个评论
Stephen23
2022-3-21
编辑:Stephen23
2022-3-21
"So I really want to sort string A against this string B. "
But that is not what you asked for, in fact your comment contradicts your original question: "where array A is the desired order and I would like to have array B placed in the same order".
So originally you stated that you want to sort B (into the order given by A).
Now you write that you really want to sort A ("against" B)..
So which is correct; do you want to sort B (your question) or A (your comment) ?
We rely on the information you write here.
PS: please do NOT repeatedly post the same question. It will not get you an answer faster, in fact it slows down getting help because it splits information over multiple threads which makes it harder for us to help you. We are not robots.
采纳的回答
Jan
2022-3-21
编辑:Jan
2022-3-21
The question is strange. Currently the best solution is:
A = ["FLOATING.NET.P2" "PDSTL" "POLY1.1" "POLY1.5" "CONT.1" "ANCH.4" ...
"MET.3" "BEAMS.3" "GERING.1" "GERING.2" "CAVE.3"];
B = ["ANCH.4" "BEAMS.3" "CAVE.3" "CONT.1" "FLOATING.NET.P2" "GERING.1" ...
"GERING.2" "MET.3" "PDSTL" "POLY1.1" "POLY1.5"];
Result = B;
Maybe A and B do not have equal entries? Then:
Result = B(ismember(B, A));
% Equivalently:
Result = intersect(B, A, 'stable')
I assume, you forgot to mention a scpecific detail. Can the strings appear multiple times in A? Then:
A = ["A", "B", "A", "C", "D", "A"];
B = ["C", "A", "B", "D"];
[~, index] = ismember(A, B);
[~, s] = sort(index);
Result = A(s)
3 个评论
Jan
2022-3-22
Oh, now an additional feature comes into play: There is a part before a colon. Please include such important details directly in the question. Adding this later in the discussion wastes your time and the one of the person, who want to help you.
B = ["ANCH.4:text here";"BEAMS.3:text here";"CAVE.3:text here"; ...
"CONT.1:text here";"FLOATING.NET.P2:text here"; ...
"GERING.1:text here";"GERING.2:text here"; ...
"MET.3:text here";"PDSTL:text here";"POLY1.1:text here"; ...
"POLY1.5:text here"];
BB = strtok(B, ':'); % Crop the string before the colon
[~, index] = ismember(BB, A); % Search cropped strings
[~, s] = sort(index);
Result = B(s) % Take values of full strings
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!