Replace zeros with text
Mostrar comentarios más antiguos
How could somebody replace zeros in a matrix or vector with a word?
For example, I'm trying to make a column n rows long that says the word 'hello'. How could I do that?
a = zeros(10,1)
a(a==0) = 'hello'
3 comentarios
"How could somebody replace zeros in a matrix or vector with a word?"
If you expect this
a = zeros(10,1)
a(a==0) = 'hello'
to produce a column vector with the word hello ten times this this is not possible with numeric arrays: numeric arrays store numeric data and they cannot contain "words" like that. Note too:
- It is possible to convert the letters to their character codes and store those in a numeric array (but this does not seem to be what you are after).
- It is possible to store words (i.e. character vectors) in a cell array.
- You can easily use a lookup table with some indices.
If you actually explain to us what you are trying to achieve then we can help you find efficient solutions to solve your task: what do you intend to do with this vector?
Dylan Mecca
el 22 de Feb. de 2018
Jan
el 22 de Feb. de 2018
@Dylan: The question might sound easy if you are not familiar with programming. For an experiences programmer zeros and strings are such completely different things, that it is hard to imagine, why you want to do this. A metaphor: Imagine you have a hand full of eggs and want to replace the eggs by sunsets. Hm.
Numbers (as zeros) are numbers and you cannot "replace" them by strings. Note that a single 0 inside a vector is one element of type double, while 'hello' is a vector of 5 elements of type char.
I will later then compare that vector to others for
true/false outputs
This sounds even more strange.
Is there a way to create a cell array that is n rows long
that contains 'hello' in each row?
Yes, this is clear. See Stephen's answer.
Respuestas (2)
James Tursa
el 21 de Feb. de 2018
"... I'm trying to make a column n rows long that says the word 'hello' ..."
a = ('hello')';
The above results in a column vector that says 'hello'. Not sure what your real goal is.
4 comentarios
Dylan Mecca
el 21 de Feb. de 2018
James Tursa
el 21 de Feb. de 2018
hello = ('hello')';
n = number of rows
a = hello(mod(0:n-1,numel(hello))+1);
Dylan Mecca
el 21 de Feb. de 2018
James Tursa
el 21 de Feb. de 2018
Editada: James Tursa
el 21 de Feb. de 2018
When you used n=11 above, I count a column of 11 rows in the output exactly as your wording requested. If you mean 11 rows of 'hello' stacked vertically, then just
a = repmat('hello',11,1);
Also, replacing char class elements into a double class variable will turn the char values into double values (the ASCII number of the character). It is not clear to me what you want for an output in this case. You can't have char class data stored inside a double class matrix. E.g.,
>> a = zeros(1,10)
a =
0 0 0 0 0 0 0 0 0 0
>> a(1:5) = 'hello'
a =
104 101 108 108 111 0 0 0 0 0
The char data simply gets converted to a double value ASCII equivalent.
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!