MATLAB Answers

replacing elements in a matrix of char

38 views (last 30 days)
xRobot
xRobot on 23 Nov 2019
Commented: Walter Roberson on 27 Nov 2019
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.

  0 Comments

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 23 Nov 2019
Try this:
this = ['axaa'];
output = strrep(this, 'x', '') % One way
output = this(this ~= 'x') % Another way

  24 Comments

Walter Roberson
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
xRobot on 27 Nov 2019
Hmm sounds very interesting. So “multiset” would be the keyword in searching the documentation? I am going to investigate this further.
Walter Roberson
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.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by