multipying character in array number

4 views (last 30 days)
best16 programmer on 21 Nov 2016
Edited: 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 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

Accepted Answer

Azzi Abdelmalek on 21 Nov 2016
repmat({'a'} ,1,5)
1 CommentShow -1 older commentsHide -1 older comments
best16 programmer on 21 Nov 2016
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.

More Answers (2)

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 CommentsShow 1 older commentHide 1 older comment
Star Strider on 21 Nov 2016
My pleasure.
Note that it also allows for ‘0’ entries in ‘v’.
James Tursa on 21 Nov 2016

Sign in to comment.

Philip Borghesani on 21 Nov 2016
Edited: 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 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

Categories

Find more on Matrix Indexing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!