Reordering string arrays if string is >9

조회 수: 1 (최근 30일)
Jason
Jason 2020년 3월 18일
댓글: Jason 2020년 3월 18일
Hello, I have a string array that has been sorted but puts 10 next to 1
b =
1×6 string array
"1" "10" "3" "5" "7" "r1"
My strings are upto 12.
I want the last element to remain where it is, and to move any strings > 9 if exist to the end -1
i.e.
"1" "3" "5" "7" "10" "r1"
and if I have
"1" "10" "12" "5" "7" "r1"
then to be
"1" "5" "7" "10" "12" "r1"
This is my attempt:
b
Id10 = find(contains(b,"10"))
if Id10>0
e=b(Id10); b(Id10)=[]; b(end-1)=e;
out{j,1}=b;
b
end
Id11 = find(contains(b,"11"))
if Id11>0
e=b(Id11); b(Id11)=[]; b(end-1)=e;
out{j,1}=b;
b
end
Id12 = find(contains(b,"12"))
if Id12>0
e=b(Id12); b(Id12)=[]; b(end-1)=e;
out{j,1}=b;
end
b
but it seems to be over writing the end-1 element
b =
1×7 string array
"1" "10" "11" "12" "3" "6" "r1"
Id10 =
2
b =
1×6 string array
"1" "11" "12" "3" "10" "r1"
Id11 =
2
b =
1×5 string array
"1" "12" "3" "11" "r1"
Id12 =
2
b =
1×4 string array
"1" "3" "12" "r1"
Thanks for any help
  댓글 수: 2
BobH
BobH 2020년 3월 18일
This statement shortens the array (same for Id11/12)
b(Id10)=[];
All you were missing is a statement to restore the size, by copying the last element onto the end.
Then you could overwrite end-1
b(end+1) = b(end); % copy last element onto end
b(Id10)=[]; % remove '10'
b(end-1) = e; % overwrite end-1
Jason
Jason 2020년 3월 18일
Perfect, thankyou very much!

댓글을 달려면 로그인하십시오.

채택된 답변

BobH
BobH 2020년 3월 18일
sort_nat is powerful, and has helped me several times.
An alternative for simple number/not-number values is straightforward
  1. find the locations of the numbers
  2. sort those
  3. append the not-numbers
z = cellfun(@(X) logical(~isempty(str2num(X))),b); % z shows locations of number strings
n = cellfun(@str2num, b(z)); % into numerical
[~, ix] = sort( n ); % ix tells what order to pull from b
r = b(ix); % fill the result using sorted numbers
r{end+1} = b{~z}; % append the not-numbers
  댓글 수: 6
Jason
Jason 2020년 3월 18일
Thanks Bob, that will always be the case for me
BobH
BobH 2020년 3월 18일
Just for completeness, for those who might have numbers anywhere, here's a more general solution
  1. find the locations of the numbers
  2. build a new numeric vector, using a placeholder number for the not-numbers. Inf or -Inf are good choices that will sort to the end or the front
  3. use the sort index to pull from b
z = cellfun(@(X) logical(~isempty(str2num(X))),b); % z shows locations of number strings
n(~z) = Inf; % placeholder value; makes not-strings sort to end
n(z) = cellfun(@str2num, b(z));
[~, ix] = sort( n );
r = b(ix);

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

dpb
dpb 2020년 3월 18일
  댓글 수: 1
Jason
Jason 2020년 3월 18일
Thanks, but Im running into an error
b =
1×6 string array
"1" "3" "6" "8" "9" "r1"
Index in position 1 exceeds array bounds.
Error in sort_nat (line 62)
num_val(i,z(i,:)) = sscanf(sprintf('%s ',digruns{i}{:}),'%f');
Error in PlateSeq2JB/LockPositionsButtonPushed (line 693)
[b,~] = sort_nat(b)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by