multipying character in array number

2 Ansichten (letzte 30 Tage)
best16 programmer
best16 programmer am 21 Nov. 2016
Bearbeitet: Philip Borghesani am 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.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 21 Nov. 2016
repmat({'a'} ,1,5)
  1 Kommentar
best16 programmer
best16 programmer am 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]

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Star Strider
Star Strider am 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 Kommentare
Star Strider
Star Strider am 21 Nov. 2016
My pleasure.
Note that it also allows for ‘0’ entries in ‘v’.
James Tursa
James Tursa am 21 Nov. 2016

Melden Sie sich an, um zu kommentieren.


Philip Borghesani
Philip Borghesani am 21 Nov. 2016
Bearbeitet: Philip Borghesani am 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]])'

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by