필터 지우기
필터 지우기

How to replace non zero value with character

조회 수: 1 (최근 30일)
Vishal Sharma
Vishal Sharma 2017년 6월 23일
편집: Jan 2017년 6월 23일
Matrix A = [0 0 1; 1 0 0;0 2 0];
I want to create another matrix replacing non zeros elements (i.e. (1,3), (2,1), (3,2) with character string, e.g. OK
Please suggest....

답변 (1개)

Stephen23
Stephen23 2017년 6월 23일
>> A = [0 0 1; 1 0 0;0 2 0];
>> C = repmat({''},size(A));
>> C(A~=0) = {'OK'}
C =
'' '' 'OK'
'OK' '' ''
'' 'OK' ''
  댓글 수: 2
Andrei Bobrov
Andrei Bobrov 2017년 6월 23일
C = cell(size(A));
C(A~=0) = {'OK'}
Jan
Jan 2017년 6월 23일
편집: Jan 2017년 6월 23일
@Vishal Sharma: Do you see that Stephen has used a cell? You cannot mix numbers and characters directly in a double array.
Or:
Pool = {'', 'OK'};
C = Pool((A~=0) + 1);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by