Character array with one letter

1 回表示 (過去 30 日間)
Jacob
Jacob 2012 年 4 月 17 日
I want to make an array with one character, and i want the user to be able to write how many. For example:
cha='hello';
cha=upper(cha);
length=length(cha);
now, lets say i want to create an array with only dots and i want it to be as many as 'length' is (in this case 5, so i want this to be shown: ..... ). How do I proceed?
thanks

回答 (3 件)

Andrei Bobrov
Andrei Bobrov 2012 年 4 月 17 日
cha='hello';
cdot = '.';
out = cdot(ones(1,numel(cha)))

the cyclist
the cyclist 2012 年 4 月 17 日
Here's one way:
L = length(cha);
A = repmat('.',[1 L])
Also, I recommend against defining a variable named "length", because it is a MATLAB command. That often leads to confusion down the road.

Matt Tearle
Matt Tearle 2012 年 4 月 17 日
Easy
char(46*ones(size(cha)))
Not so easy
regexprep(char,'\w','.')
:)

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by