Scramble words in MATLAB.
Afficher commentaires plus anciens
hi my friends. im looking for if there is any way to scramble word in MATLAb. exactly i want that a function returns a string containing the words in the original string in random order and also i wannaa use randperm to do tthat. i wrote this function but i was not succesful.
function [sout] = scrambleWords(s)
%for Non-Functional use%
%s = input('Enter a few Words:\n','s');
cutPoints = regexp(s,'\s{1,}'); % for More than 1 Space=> '\s{2,}'
sout = mat2cell(s,1,diff([0 cutPoints numel(s)]));
sout = randperm(sout,10,3);
end
Réponses (3)
Mischa Kim
le 14 Jan 2014
Modifié(e) : Mischa Kim
le 14 Jan 2014
See below:
function [permsout] = scrambleWords(s)
cutPoints = regexp(s,'\s{1,}'); % for More than 1 Space=> '\s{2,}'
sout = mat2cell(s,1,diff([0 cutPoints numel(s)]));
perm = randperm(length(sout));
permsout = sout(perm);
end
As a last step you could convert the cell array back to a string and return by the function.
ADiNoS
le 14 Jan 2014
1 commentaire
Mischa Kim
le 14 Jan 2014
Modifié(e) : Mischa Kim
le 14 Jan 2014
You are probably still working with some old code. Copy and paste the function in my answer and save under a new function name. This is what I get:
>> scrambleWords('this should really work')
ans =
'work' 'should ' 'this ' 'really '
>> scrambleWords('this should really work')
ans =
'should ' 'really ' 'this ' 'work'
>> scrambleWords('this should really work')
ans =
'should ' 'this ' 'really ' 'work'
Catégories
En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!