[英]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.