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

4 visualizaciones (últimos 30 días)
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 comentarios
José-Luis
José-Luis el 6 de Jul. de 2017
What would the resulting matrix look like?
Stephen23
Stephen23 el 6 de Jul. de 2017
Editada: Stephen23 el 6 de Jul. de 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?

Iniciar sesión para comentar.

Respuestas (1)

Guillaume
Guillaume el 6 de Jul. de 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) +'*'

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by