multipying character in array number

2 次查看(过去 30 天)
i want to create a character array for example if a.*[1 1 1 1] we get [a a a a] thank you.

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2016-11-21
repmat({'a'} ,1,5)
  1 个评论
best16 programmer
best16 programmer 2016-11-21
thank you. but what if we have a.*[1 -1 1 -1 -1] how can we get [a -a a -a -a]

请先登录,再进行评论。

更多回答(2 个)

Star Strider
Star Strider 2016-11-21
‘what if we have a.*[1 -1 1 -1 -1] how can we get [a -a a -a -a]’
That requires a loop, but it works:
v = [1 -1 1 -1 -1];
a_vec = [];
for k1 = 1:length(v)
if v(k1) > 0
a_vec = [a_vec ' a '];
elseif v(k1) < 0
a_vec = [a_vec '-a '];
elseif v(k1 == 0)
a_vec = [a_vec ' 0 '];
end
end
a_vec =
a -a a -a -a
  3 个评论
Star Strider
Star Strider 2016-11-21
My pleasure.
Note that it also allows for ‘0’ entries in ‘v’.

请先登录,再进行评论。


Philip Borghesani
Philip Borghesani 2016-11-21
编辑:Philip Borghesani 2016-11-21
Did you really want a character array?
>> syms a
>> b=a.*[1 -1 1 -1 -1]
b =
[ a, -a, a, -a, -a]
To get a character array you can do
>> char(b)
ans =
'matrix([[a, -a, a, -a, -a]])'

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by