Sorting a string by the use of another predetermined string order

조회 수: 7 (최근 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 CenterFile Exchange에서 Shifting 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