繁体   English   中英

在MATLAB中使用迭代解码消息

[英]Using Iteration to Decode a Message in MATLAB

因此,我有点为这个作业而烦恼。 尽管这并不是什么新鲜事,但是我使用循环只是感觉很好,但是将迭代添加到混合中会使我失望。 无论如何,我想要帮助的是在MATLAB中解码一条消息。 有两种不同类型的消息:一种包含一堆字符,另一种包含空格(因此是一个实际的句子)。 如果我有一个字符串,我必须找到每个第n个字符,如果是一个字符串,我必须找到每个第n个字符。

function[code]= decodeStr(str, index)
    code = [];  %// Starts me off with an empty vector to populate
    word_in_message = strsplit(str, ' ');  %// Splits up my vector
    word_in_message = char(word_in_message); %//Turns it into a character, which I need
    find_spaces = find(str == ' '); %// Finds spaces, how I know it's not a character string
    words = (find_spaces + 1); %//Index es the word, but not too sure I need this
    Counter = 1; %// I plan to use this in the character string

    while index <= length(str)
        if find_spaces > 0 %// If it is a string with words, they'll be spaces
            code = word_in_message(index, :); %// This works in the command window. 
            code = [str(index)];
        end
        index = index + 1 ;
    end

end

如您所见,我很迷失。 我知道我可以在命令窗口中使用words_in_message(index,:)来获取完整的单词,但是它在函数中出错。 它会抱怨超出尺寸,尽管我可以知道为什么会发生这种情况(索引=索引+1是罪魁祸首),但我不确定该如何解决。 我只是不断获取它,以将整个字符串反馈给我,这不是我想要的。 有什么建议么?

TestCases
 str = ['I want to go  to the   track meet and eat a Jimmy Johns ''gourmet sandwich ...
 on the first with    Wednesday Adams.'];
 [out1] = decodeStr(str,4)
      => out1 = 'go meet Jimmy on Wednesday'
[out2] = decodeStr('www.harttoregerill.com/op',3)
          => out2 = 'waterloo'

抱歉,这似乎有点含糊。 我的腕管正张开,所以我尽量避免输入太多:/

function code = decodeStr(str, index)
    if find(str == ' ', 1)
        words = strsplit(str, ' '); 
        code = strjoin(words(index : index : end));
    else
        code = str(index : index : end);
    end;
end

我没有足够的代表对此发表评论:@Cheery

第4行: code = strjoin(words(index : index : end));

编辑:函数strjoin的默认定界符是空格,无需指定它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM