Sorting a string by the use of another predetermined string order
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Stephen23
le 21 Mar 2022
Modifié(e) : Stephen23
le 21 Mar 2022
"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.
Réponse acceptée
Jan
le 21 Mar 2022
Modifié(e) : Jan
le 21 Mar 2022
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 commentaires
Jan
le 22 Mar 2022
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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!