Skip to content

Instantly share code, notes, and snippets.

@srishtis
Created August 26, 2018 09:20
Show Gist options
  • Save srishtis/8a14d79a6f2aa31cefccc14261eaa3fe to your computer and use it in GitHub Desktop.
Save srishtis/8a14d79a6f2aa31cefccc14261eaa3fe to your computer and use it in GitHub Desktop.
Code for removing short words from the input string
% This function removes words that have length shorter than thresh_len (in this snippet: 3)
% It also removes extra spaces
function words_new = remove_small_words(words)
temp1 = strsplit(words, ' ');
words_new='';
for i = 1:numel(temp1)
k = temp1{i};
a=length(k);
thresh_len= 3;
if a<thresh_len
k='';
wht_space= ' ';
words_new = [words_new,wht_space,k];
else
wht_space= ' ';
words_new = [words_new,wht_space,k];
end
end
words_new= strtrim(words_new);
words_new= regexprep(words_new,' +',' ');
end
%%Example:
% For::
%words = 'ours is not to reason why';
%words_new = remove_small_words(words);
% output: words_new= 'ours not reason why'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment