How to replace a double in a array with a string (cell)?

I have a matrix of alternating 1s and 0s. Such as A = 1 0 1 0 1 0 0 1 0 1 0 1
I want to replace the "1" with a string called "[apple]" including the brackets.
How should I do this? It keeps giving me errors.

Respuestas (2)

A = [1 0 1 0 1 0 0 1 0 1 0 1];
Ac = num2cell(A);
Ac(A==1) = {'[apple]'};
If you wanted to replace 1 with [apple] and 0 with something else you could do
str_table = {'[orange]', '[apple]'};
Ac = str_table(A+1);
A = [1 0 1 0 1 0 0 1 0 1 0 1];
out = cell(size(A));
out(A>0) = {'[apple]'};

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Preguntada:

el 2 de Oct. de 2015

Comentada:

el 3 de Oct. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by