lundi 22 février 2016

Function to scramble letters of a string in MATLAB using randperm

I have to write a function that shuffles letters, using randperm. I have already tried to look up some answers here on this forum, but I have just started programming so I am not completely able to apply those answers to my own function.

function [ shuffled_word ] = letter_shuffler( a )
%UNTITLED2 Summary of this function goes here
%   Detailed explanation goes here

shuffled_word = a(randperm(length(a)))

end

If I try a = 'bacon' a = a(randperm(length(a))) in my command window it shuffles the letters. Why doesn't it work in my function?

I also tried to write a function that should do the same, but takes a string of any length as input {'apple', 'pie', 'banana'}:

function [ shuffled_word ] = shuffle_letters( s )
%UNTITLED4 Summary of this function goes here
%   Detailed explanation goes here

shuffled_word = []

for ii = length(s)
total_letters = length(s{ii})
shuffled_numbers = randperm(total_letters)

end

end

I haven't finished this code yet, because I don't know how to index back to letters.

Is there someone who can help me with one of the two codes?




Aucun commentaire:

Enregistrer un commentaire