why ascii instead of letter?

3 次查看(过去 30 天)
huda nawaf
huda nawaf 2011-9-19
hi,
I want place 'a' instead of 1 as in :
x=[1 2 3 1]; >> for i=1:4 if x(i)==1 x(i)='a' end end
but the result: 97 2 3 97
why ascii is appearing?

回答(2 个)

Walter Roberson
Walter Roberson 2011-9-19
You have initialized a numeric array rather than a character array. Numeric arrays cannot hold characters as characters. Only cell arrays can mix numerics with characters.

Fangjun Jiang
Fangjun Jiang 2011-9-19
Original x is a double array, but in your for-loop, you assign a char value to its element. So it takes the ASCII value of letter 'a'. see
double('a')
  2 个评论
huda nawaf
huda nawaf 2011-9-20
so , what I have to do?
thanks
Fangjun Jiang
Fangjun Jiang 2011-9-20
use cell array
%%
x={1 2 3 1};
for i=1:4
if x{i}==1
x{i}='a';
end
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Operators and Elementary Operations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by