[英]Using Iteration to Decode a Message in MATLAB
So I am kind-of stuck on this problem for homework. 因此,我有点为这个作业而烦恼。 Though that's nothing new, I felt fine using just loops, but adding iteration into the mix is throwing me off.
尽管这并不是什么新鲜事,但是我使用循环只是感觉很好,但是将迭代添加到混合中会使我失望。 Anyway, what I would like help with is decoding a message in MATLAB.
无论如何,我想要帮助的是在MATLAB中解码一条消息。 There are two different types of messages: one that involves a bunch of characters, and one that involves spaces(so an actual sentence).
有两种不同类型的消息:一种包含一堆字符,另一种包含空格(因此是一个实际的句子)。 If I have a string of characters, I have to find every nth character, if it's a string of characters, I have to find every nth character.
如果我有一个字符串,我必须找到每个第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
As you can see, I am sadly lost. 如您所见,我很迷失。 I know I can use words_in_message(index, :) in the command window to get the full word, but it errors out in the function.
我知道我可以在命令窗口中使用words_in_message(index,:)来获取完整的单词,但是它在函数中出错。 It'll complain about exceeding the dimensions and while I can see why that happens (the index = index + 1 being the culprit), I am unsure of what to do to fix it.
它会抱怨超出尺寸,尽管我可以知道为什么会发生这种情况(索引=索引+1是罪魁祸首),但我不确定该如何解决。 I just keep getting it to feed back the whole string to me, which is not what I wanted.
我只是不断获取它,以将整个字符串反馈给我,这不是我想要的。 Any suggestions?
有什么建议么?
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'
Sorry if this seems a bit vague. 抱歉,这似乎有点含糊。 My carpal tunnel is flaring up, so I'm trying to avoid typing a lot :/
我的腕管正张开,所以我尽量避免输入太多:/
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
I don't have enough rep to comment it so: @Cheery 我没有足够的代表对此发表评论:@Cheery
line 4: code = strjoin(words(index : index : end));
第4行:
code = strjoin(words(index : index : end));
Edit : the default delimiter of function strjoin is space, no need to specify it 编辑:函数strjoin的默认定界符是空格,无需指定它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.