Sorting a string by the use of another predetermined string order

17 Ansichten (letzte 30 Tage)
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 Kommentare
Justin Brangiforte
Justin Brangiforte am 21 Mär. 2022
So I really want to sort string A against this string B. Where before the colon is the only thing that matches string A and as I said before the array strings will always have equivalent entries, and will NEVER have duplicate entries.
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"];
Stephen23
Stephen23 am 21 Mär. 2022
Bearbeitet: Stephen23 am 21 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 21 Mär. 2022
Bearbeitet: Jan am 21 Mär. 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')
Result = 1×11 string array
"ANCH.4" "BEAMS.3" "CAVE.3" "CONT.1" "FLOATING.NET.P2" "GERING.1" "GERING.2" "MET.3" "PDSTL" "POLY1.1" "POLY1.5"
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)
Result = 1×6 string array
"C" "A" "A" "A" "B" "D"
  3 Kommentare
Justin Brangiforte
Justin Brangiforte am 21 Mär. 2022
So I really want to sort string A against this string B. Where before the colon is the only thing that matches string A and as I said before the array strings will always have equivalent entries, and will NEVER have duplicate entries.
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"];
Jan
Jan am 22 Mär. 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by