how to I write a code for this problem:
Write a program that accepts a string from a user with the input function, chops that string into a series of tokens, sorts the tokens into ascending order, and prints them out.
this is what I wrote so far I dont know what to do
%Input a user string
str=input('Enter string:','s');
[token,remain]=strtok(str,',')

댓글 수: 2

ANKUR KUMAR
ANKUR KUMAR 2021년 3월 17일
Could you please elaborate "chops that string into a series of tokens"? Where comes the token in the string?
Aminata camara
Aminata camara 2021년 3월 17일
that what the problem is saying, I understand it like when I'm writting a series of world
example if a user enter : anna,mimi,jen
the user will get
'anna'
'mimi'
'jen'
also I dont get what the problem is saying by then sort that information.

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

답변 (1개)

ANKUR KUMAR
ANKUR KUMAR 2021년 3월 17일

0 개 추천

str=input('Enter string:','s');
strsplit(str,',')
On which basis you wish to sort strings? Is it alphabatically, or based on the length ?

댓글 수: 9

Aminata camara
Aminata camara 2021년 3월 17일
Thank you so much, alphabatically also what if I want to do it with length can you please show me both ways
Hint:
A = {{'wawa', 'zone'}; {'snark', 'bellman', 'banker'}; {'zoiks'}};
for K = 1 : length(A); disp(A{K}); end
{'wawa'} {'zone'} {'snark'} {'bellman'} {'banker'} {'zoiks'}
B = [2, 3, 1]
B = 1×3
2 3 1
[~, idx] = sort(B)
idx = 1×3
3 1 2
C = A(idx);
for K = 1 : length(C); disp(C{K}); end
{'zoiks'} {'wawa'} {'zone'} {'snark'} {'bellman'} {'banker'}
Now suppose B was the length() of each entry in A...
If you wish to sort alphabetically, you can use sort command:
sort(strsplit(str,','))
Aminata camara
Aminata camara 2021년 3월 17일
Thank you so much ANKUR KUMAR thanks a lot. I have a lastly question what if I want to sort by length?
a_split = strsplit(str,',');
[~,strlen] = sort(cellfun(@length,a_split),'ascend');
a_sort = a_split(strlen);
Walter Roberson
Walter Roberson 2021년 3월 17일
In my example, how do you suppose you might be able to set B to be a vector equal to the length of each cell in A ?
Because once you have that, then my example is sorting by that value.
Aminata camara
Aminata camara 2021년 3월 17일
Thank you a lot Walter Roberson, I just started Matlab learning and some in the example you gave, I'm a little confused about [~,idx] and A(idx); I highly appreciate your help. Thank you again
B = [8, -2, 11]
B = 1×3
8 -2 11
[sortedB, idx] = sort(B)
sortedB = 1×3
-2 8 11
idx = 1×3
2 1 3
So the first output of sort(B) is the list of sorted values (by default in ascending order.)
The second output of sort() tells you where each of the outputs came from in the original vector. So where the first entry, idx(1) is 2, that tells you that B(2) was the value that sorted into first place. The second entry, idx(2) is 1, telling you that B(1) was the entry that sorted second. The third entry, idx(3) is 3, telling you that B(3) is the entry in B that sorted third.
When you have that kind of order information, you can use it to index another array, and the result is reordering the other array to have order corresponding (increasing) B.
The syntax
[~, idx] = sort(B)
means the same as
[AnInternalVariableNameIsHere, idx] = sort(B);
clear AnInternalVariableNameIsHere
That is, the output is generated, but it is thrown away immediately. Using ~ is a short way to ignore an output when you need to access a later output.
Aminata camara
Aminata camara 2021년 3월 18일
ooh I see now thank you for the great explanation

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

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

질문:

2021년 3월 17일

댓글:

2021년 3월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by