replacing elements in a matrix of char
Show older comments
Is there a function similar to string replace for char's? For instance in the code below I would like to remove the element 'x' in "this" and then display it again without the x.
this = ['axaa'];
ans =
aaa
I am thinking I would possibly have to make an empty char array and then append to it? Thanks for any advice.
Answers (1)
Image Analyst
on 23 Nov 2019
Try this:
this = ['axaa'];
output = strrep(this, 'x', '') % One way
output = this(this ~= 'x') % Another way
24 Comments
xRobot
on 23 Nov 2019
xRobot
on 23 Nov 2019
xRobot
on 23 Nov 2019
xRobot
on 23 Nov 2019
xRobot
on 23 Nov 2019
Image Analyst
on 23 Nov 2019
Well now I'm confused. Do you want to get rid of a whole word ("any four letter word"), or one or more letters within the words?
What is your current code?
If you delete letters such that your strings are not all the same length, you'll have to use cell arrays, not string arrays. For example
>> scrambled = ['oodd'; 'haaa'; 'aza';]
Dimensions of arrays being concatenated are not consistent.
is not allowed since "aza" is not 4 characters like all the other entries.
Walter Roberson
on 23 Nov 2019
Edited: Walter Roberson
on 23 Nov 2019
char(regexprep(string(scrambled), 'a', ' ', 'once')) % Replace one 'a' with a space.
xRobot
on 24 Nov 2019
Image Analyst
on 24 Nov 2019
char() is not being assigned to anything. You need to assign it to some variable, which you then check.
What do you expect as the output. For example if I enter "d" do you want to omit the first word from the output? Like this:
scrambled = {'oodd'; 'haaa'; 'aaaa'}
disp(scrambled);
guess = input('Enter guess: ','s')
fprintf('\n');
numWords = length(scrambled)
rowsToKeep = true(numWords, 1);
for k = 1 : length(scrambled)
thisWord = scrambled{k};
if contains(thisWord, guess)
rowsToKeep(k) = false;
end
end
output = scrambled(rowsToKeep)
Output in command window:
scrambled =
3×1 cell array
{'oodd'}
{'haaa'}
{'aaaa'}
Enter guess: d
guess =
'd'
numWords =
3
output =
2×1 cell array
{'haaa'}
{'aaaa'}
xRobot
on 24 Nov 2019
Image Analyst
on 24 Nov 2019
It removed the entire string because you said "It would be optimum to delete the entire phrase" so that's what I did. Now I'm confused again. Explain in detail why, if the word is "oodd" and the guess word is "hood" how you end up with only a single "d". If you're only removing all but the last occurrence, then why is an "o" also not left behind? Why just the "d" is left behind?
xRobot
on 24 Nov 2019
Edited: Image Analyst
on 24 Nov 2019
Image Analyst
on 24 Nov 2019
This will do it:
scrambled = {'oodd'; 'haaa'; 'aaaa'}
disp(scrambled);
% guess = input('Enter guess: ','s')
guess = 'hood';
fprintf('\n');
numWords = length(scrambled)
output = scrambled; % Initialize
for k = 1 : length(scrambled)
thisWord = output{k};
for k2 = 1 : length(guess)
thisLetter = guess(k2);
index = find(thisWord == thisLetter, 1, 'first')
thisWord(index) = '';
end
output{k} = thisWord; % Put back in the possibly altered word.
end
output % Show in command window.
xRobot
on 24 Nov 2019
Image Analyst
on 24 Nov 2019
'haaa' went to 'haa', and 'aaaa' went to 'aaa' so to me it looks like it's removing "a" once per word. Why do you say it's removing more than 1 'a'?
xRobot
on 24 Nov 2019
Image Analyst
on 25 Nov 2019
What do you mean by "block"? I done have any. I have character arrays (simple strings), and cell arrays (arrays of chracter arrays). Do you want to only remove the first occurrence of a character,in the first cell that it appears in, regardless of how many cells it appears in?
Please review the FAQ on cell arrays
xRobot
on 25 Nov 2019
xRobot
on 25 Nov 2019
Image Analyst
on 25 Nov 2019
I don't know - perhaps. I didn't even know that count function existed. But wonderful, now another built-in MATLAB function that uses a common name that we'll have to take care not to use as a variable name.
xRobot
on 26 Nov 2019
Edited: Walter Roberson
on 26 Nov 2019
Walter Roberson
on 26 Nov 2019
I would suggest that you should be considering using a "multiset". A multiset is similar to a set, except that each element has an associated count. A target word can still be formed if its multiset is a (multiset) subset of the available letters.
xRobot
on 27 Nov 2019
Walter Roberson
on 27 Nov 2019
MATLAB itself does not have much support for multisets built in -- just some obscure parts of the internal symbolic engine, https://www.mathworks.com/help/symbolic/mupad_ref/dom-multiset.html . But multiset is the common mathematics term.
Categories
Find more on Common Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!