How to replace non zero value with character

2 vues (au cours des 30 derniers jours)
Vishal Sharma
Vishal Sharma le 23 Juin 2017
Modifié(e) : Jan le 23 Juin 2017
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....

Réponses (1)

Stephen23
Stephen23 le 23 Juin 2017
>> A = [0 0 1; 1 0 0;0 2 0];
>> C = repmat({''},size(A));
>> C(A~=0) = {'OK'}
C =
'' '' 'OK'
'OK' '' ''
'' 'OK' ''
  2 commentaires
Andrei Bobrov
Andrei Bobrov le 23 Juin 2017
C = cell(size(A));
C(A~=0) = {'OK'}
Jan
Jan le 23 Juin 2017
Modifié(e) : Jan le 23 Juin 2017
@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);

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by