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

6 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2017-7-6
编辑:Stephen23 2017-7-6
"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?

请先登录,再进行评论。

回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by