how to insert an asterisks on some elements of a matrix that meet condition?

4 views (last 30 days)
I am trying to insert asterisks on particular elements of a matrix. Say for example that
A=[2 4; 34 1]
A(find(A<3))=A(find(A<3))+'*'
How may I do so
  2 Comments
Stephen23
Stephen23 on 6 Jul 2017
Edited: Stephen23 on 6 Jul 2017
"I am trying to insert asterisks on particular elements of a matrix"
What does that mean? Numeric arrays contain numeric values following IEEE 754: this does not include asterisks, alphabet characters, smileys, youtube videos, or anything else that users might wish for. What do you imagine the output should be?

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 6 Jul 2017
Matrices in matlab store numbers and numbers only. 2* is not a number and therefore cannot be stored in a matrix.
The only workaround would be to transform your numbers into character arrays (or string arrays in newer versions) but that would significantly complicate any mathematical calculation since you're not dealing with numbers anymore
Using old fashioned char arrays:
A=[2 4; 34 1];
Achar = arrayfun(@num2str, A, 'UniformOutput', false)
Achar(A < 3) = cellfun(@(c) [c, '*'], Achar(A < 3), 'UniformOutput', false)
Using strings (requires R2016b or later)
A=[2 4; 34 1];
Astring = string(A);
Astring(A < 3) = Astring(A < 3) +'*'

Categories

Find more on Get Started with MATLAB 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!