How to make a connection between a key word and a binary digit?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
I have a text 'mail.txt' and I chose a key word 'good'which is repeated 10 times in the text,I have generated a random binary number consists of 10 digits 0100111011 ,the first occurrence of the word 'good' meets the first digit'0' the second occurrence meets digit'1' and so on,How to achieve this and also to say if the the digit is '1' replace 'good'with 'bad' ,else no changes,can I have your help please.
Accepted Answer
Walter Roberson
on 4 Feb 2017
0 votes
strfind() 'good', giving back a vector of starting indices. Use the binary vector to select elements out of that array. Make the replacement at each remaining location.
Hint: try looping backwards
11 Comments
jojototo
on 6 Feb 2017
Thanks,but what is meant by looping backwards?
Walter Roberson
on 6 Feb 2017
for K = 10 : -1 : 1
instead of
for K = 1 : 10
jojototo
on 11 Feb 2017
thanks
jojototo
on 11 Feb 2017
Edited: Walter Roberson
on 11 Feb 2017
How to Use the binary vector to select elements out of that array?
Walter Roberson
on 11 Feb 2017
selected_locs = '0100111011' == '1';
nloc = length(selected_locs);
idx = strfind(S, 'good');
nix = length(idx);
if nix > nloc; selected_locs(nix) = false; end
if nix < nloc; selected_locs(nix+1:end) = []; end
places_to_change = idx(nix);
Walter Roberson
on 14 Mar 2017
for loc = fliplr(places_to_change)
S = [S(1:loc-1), 'bad', S(loc+4:end)];
end
jojototo
on 26 Mar 2017
I have a problem with the code you have sent ,only the last occurance of the word "good"is replaced by the word"bad" and also process of replacement doesn't depend on the binary digits "if 1 make the replacement and if 0 don't do the replacement" thanks a lot for your efforts
Walter Roberson
on 26 Mar 2017
places_to_change = idx(selected_locs);
jojototo
on 31 Mar 2017
Thank you so much,it works
jojototo
on 4 Apr 2017
Hi , the code works,but there is a problm that when the word that I want to replace it is a part of another word"like 'the' in 'there,these,rather'"the letters of 'the' in these words also replaced ,I want only the word "the" to be replaced. I tried this code instead of strfind but also doesn't work >>idx = regexpi(A, 't[h]+e'); thanks for your efforts.
Walter Roberson
on 4 Apr 2017
To go further, you need to define what a "word" is. In English that is not easy.
The below is an example I used in 2008 for someone wanting to separate out paragraphs. Which are the words in this?
Mr. Todd E. Jones gave $3000. (!) in nickels, dimes, etc. to his
No. 1 son at 4 7th Ave. N., NY. NY. USA., who exclaimed "What joy!
Now I can buy 3 lbs. of St. Tropiz bananas... or can I?!"
More Answers (0)
Categories
Find more on Characters and Strings in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)