Sorting a string by the use of another predetermined string order

6 ビュー (過去 30 日間)
Justin Brangiforte
Justin Brangiforte 2022 年 3 月 21 日
コメント済み: Jan 2022 年 3 月 22 日
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 件のコメント
Justin Brangiforte
Justin Brangiforte 2022 年 3 月 21 日
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 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
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')
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 件のコメント
Justin Brangiforte
Justin Brangiforte 2022 年 3 月 21 日
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 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 ExchangeShifting and Sorting Matrices についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by