MATLAB Answers

0

multipying character in array number

Asked by best16 programmer on 21 Nov 2016
Latest activity Edited by Philip Borghesani on 21 Nov 2016
i want to create a character array for example if a.*[1 1 1 1] we get [a a a a] thank you.

  0 Comments

Sign in to comment.

3 Answers

Answer by Azzi Abdelmalek
on 21 Nov 2016
 Accepted Answer

repmat({'a'} ,1,5)

  1 Comment

thank you. but what if we have a.*[1 -1 1 -1 -1] how can we get [a -a a -a -a]

Sign in to comment.


Answer by Star Strider
on 21 Nov 2016

‘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 Comments

thank you ,it works.how we can generate the vector v in a random way.
My pleasure.
Note that it also allows for ‘0’ entries in ‘v’.

Sign in to comment.


Answer by Philip Borghesani on 21 Nov 2016
Edited by Philip Borghesani on 21 Nov 2016

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]])'

  0 Comments

Sign in to comment.