필터 지우기
필터 지우기

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

조회 수: 6 (최근 30일)
Rami
Rami 2017년 7월 6일
편집: Stephen23 2017년 7월 6일
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
José-Luis
José-Luis 2017년 7월 6일
What would the resulting matrix look like?
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