Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用randperm?在matlab中洗牌单词中字母的函数??从非函数格式更改_Matlab_Function_Shuffle - Fatal编程技术网

使用randperm?在matlab中洗牌单词中字母的函数??从非函数格式更改

使用randperm?在matlab中洗牌单词中字母的函数??从非函数格式更改,matlab,function,shuffle,Matlab,Function,Shuffle,我有以下步骤来洗牌单词中的字母,但我需要将其更改为一个函数的形式,该函数要求用户输入一个单词,然后输出洗牌后的单词。 我该怎么做 word = input('type a word to be scrambled: ', 's'); word(randperm(numel(word))) 你必须对单词加扰的代码确实是正确的。要执行您要求的操作,您实际上只需对上述代码进行一次更改。只需将函数声明放在.m文件中,并将其称为类似于scramble.m。然后做: function word = scr

我有以下步骤来洗牌单词中的字母,但我需要将其更改为一个函数的形式,该函数要求用户输入一个单词,然后输出洗牌后的单词。 我该怎么做

word = input('type a word to be scrambled: ', 's');
word(randperm(numel(word)))

你必须对单词加扰的代码确实是正确的。要执行您要求的操作,您实际上只需对上述代码进行一次更改。只需将函数声明放在
.m
文件中,并将其称为类似于
scramble.m
。然后做:

function word = scramble
    word = input('type a word to be scrambled: ', 's');
    word(randperm(numel(word)))
end
当您调用函数时,应该将单词作为字符串输出。因此,保存此文件,然后在命令提示符下键入:

>> word = scramble;
这应该要求您输入要加扰的单词,加扰后返回该单词。该单词存储在MATLAB工作区的变量
word


一些人建议你阅读:


MathWorks的文档非常好,尤其是语法。阅读上面的链接,了解有关如何定义和使用函数的更多详细信息,但其要点是我如何在上面完成的。

matlab函数的一般格式是

    function output = MyFunctionName(input)
    ... code using 'input' goes here
    end % 
如果有多个输出,可以将它们放在一个数组中,最好用逗号分隔。如果您有多个输入,请列出它们,并用逗号分隔:

    function [out1, out2,...,outN] = MyFunctionName(input1, input2,...,inputN)
    ... code using the inputs goes here
    end % 
对于您的问题,您没有将单词传递给函数,因此函数调用不需要输入,但您需要从函数内部输入单词。这里有一个方法

    function word = ShuffleLetters
    % Output: 'word' that is shuffled within function
    % Input: none 
    word = input('type a word to be scrambled: ', 's');
    word = word(randperm(numel(word)));
    end
下面是一个示例用法:

>
shuffletters
键入要炒的单词:培根

ans=

邦卡

最后,输入和输出是可选的。这个m函数只打印“嗨!”

    function SayHi
    disp('Hi!')
    end

@克拉克-没问题。祝你好运