Replace zeros with text

19 次查看(过去 30 天)
Dylan Mecca
Dylan Mecca 2018-2-21
编辑: Stephen23 2018-2-22
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 个评论
Dylan Mecca
Dylan Mecca 2018-2-22
I want to create a column vector of n amount of zeros. Then, replace those zeros with the word, 'hello'. I don't know how much simpler to ask that question. Replace zeros with 'hello'. If that isn't possible using a numeric array, is there another way?
The end goal is to make a column vector, n rows long, that says the word 'hello'. I will later then compare that vector to others for true/false outputs... but that is not applicable in this case. Is there a way to create a cell array that is n rows long that contains 'hello' in each row?
Jan
Jan 2018-2-22
@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.

请先登录,再进行评论。

回答(2 个)

Stephen23
Stephen23 2018-2-22
"Is there a way to create a cell array that is n rows long that contains 'hello' in each row?"
repmat({'hello'},n,1)
  2 个评论
Jan
Jan 2018-2-22
Or alternatively:
c = cell(n, 1);
c(:) = {'hello'}
Stephen23
Stephen23 2018-2-22
编辑:Stephen23 2018-2-22
Or
C = {'hello'};
D = C(ones(n,1))

请先登录,再进行评论。


James Tursa
James Tursa 2018-2-21
"... 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 个评论
Dylan Mecca
Dylan Mecca 2018-2-21
when using n = 11, I get:
a =
h
e
l
l
o
h
e
l
l
o
h
This doesn't solve how to make 'hello' repeat for n amount of rows, nor address how to replace zeros.
James Tursa
James Tursa 2018-2-21
编辑:James Tursa 2018-2-21
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by