How do I repeat a character n times?

조회 수: 104 (최근 30일)
MSQLBK
MSQLBK 2017년 2월 23일
댓글: MSQLBK 2017년 2월 23일
Freq=[s0,s1,s2,s3,s4,s5,s6,s7,s8,s9];
hist=[1,length(Freq)];
for k=1:length(Freq)
hist(k)=repmat('*',Freq(k));
end
I want to create a row vector where every element in 'hist' has '*' whose quantity corresponds to the elements from the array 'Freq'. If Freq(5)=6 then hist(5) = '******'

채택된 답변

Peter O
Peter O 2017년 2월 23일
Because your histogram counts will result in variable length strings of asterisks, you'll need to use cell arrays to store each string.
Freq=[s0,s1,s2,s3,s4,s5,s6,s7,s8,s9];
hist=cell(1,length(Freq));
for k=1:length(Freq)
hist{k}=repmat('*',1,Freq(k));
end
Access the k-th string using hist{k} (note the curly braces).
  댓글 수: 1
MSQLBK
MSQLBK 2017년 2월 23일
Thank You! I had tried to use cell array previously but I was enclosing {'*'} rather than k.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2017년 2월 23일
You will need to use
hist = strings(1,length(Freq));
and you will need to use R2016b or newer.
R2016a and older cannot store multiple characters in single location that is accessed using () indexing.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by