Effacer les filtres
Effacer les filtres

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

6 vues (au cours des 30 derniers jours)
Rami
Rami le 6 Juil 2017
Modifié(e) : Stephen23 le 6 Juil 2017
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 commentaires
José-Luis
José-Luis le 6 Juil 2017
What would the resulting matrix look like?
Stephen23
Stephen23 le 6 Juil 2017
Modifié(e) : Stephen23 le 6 Juil 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?

Connectez-vous pour commenter.

Réponses (1)

Guillaume
Guillaume le 6 Juil 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) +'*'

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by