필터 지우기
필터 지우기

Need help with sorting a string when a numeric vector moves also . Can't use the sort or min function

조회 수: 1 (최근 30일)
So im trying to sort a string basec on how to Times variable moves in Task 2, i figured out how to sort the Times in ascending order but i want the Names variable to move concurrent with where the Times numbers go can anyone help?
%Task 1
load('Processes_4.mat');
[ROW,COL] = size(Names);
[r,c]= size(Times);
str1 = isvector(Names);
str=isstring(Names);
vect= isnumeric(Times);
vect1 =isvector(Times);
if vect~=1 || vect1 ~=1 || r ==1 && c ==1
Times_ =1;
else
Times_ =0;
end
if str1 ~=1 || str ~=1 || ROW ==1 && COL ==1
names_ =1;
else
names_ =0;
end
if Times_ == 1 && names_ ==1
error('Both provided variables is invalid')
end
if str~=1 || vect1 ~=1 || r==1 && c==1
error('Times variabble is invalid')
end
if str~= 1 || str~=1 || ROW ~=1 && COL ~=1
error('Names variable is invalid');
end
%Task 2
T= length(Times);
N=length(Names);
for i = 1:T
for j= 1:T
if Times(j) > Times(i)
Newtime=Times(j);
Times(j) = Times(i);
Times(i) =Newtime;
if Names(Times(j)) > Names(Times(i))
Newname= Names(Times(j));
Names(Times(j)) = Names(Times(i));
Names(Times(i)) =Newname;
end
end
end
end

답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 4월 4일
편집: Ameer Hamza 2020년 4월 4일
I cannot run your code with the mat file, so it is unclear what you are trying to do. But if you want to sort a string array, in the same order as a numeric array then try something like this
str = ["dog", "fox", "Quick", "brown", "jumps", "over", "lazy", ];
num = [7 3 1 2 4 5 6];
A = [num' (1:numel(num))'];
A(num,:) = A;
str = str(A(:,2));
  댓글 수: 4
Ameer Hamza
Ameer Hamza 2020년 4월 4일
Ah! I missed that you don't want to use sort() function. Check the updated answer.
Ameer Hamza
Ameer Hamza 2020년 4월 4일
편집: Ameer Hamza 2020년 4월 4일
Note that this will only work if the numeric vectors have all numbers from 1 to n.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by