Create a string array (MxN) where each element is a repeated character based off a numeric array (MxN)

조회 수: 3(최근 30일)
Carl
Carl 2019년 11월 25일
댓글: Carl 2019년 11월 25일
I want to create a string array (MxN) of a repeated character. Each element of the string array has the character repeated based on a number in a numeric array (MxN).
For example, if I had a numeric array as such:
A =
4 0
1 8
And I wanted to repeat the "#" character, I want to create a string array like this:
B =
2×2 string array
"####" ""
"#" "########"
The actual array I want to do this on is 7500x10 so other than a nested for-loop is there a more elegant way of getting my desired result?
Thanks
Carl

채택된 답변

Bhaskar R
Bhaskar R 2019년 11월 25일
B = string(cellfun(@(x)repelem('#', x), num2cell(A), 'UniformOutput', false));
  댓글 수: 3

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

추가 답변(1개)

Philippe Lebel
Philippe Lebel 2019년 11월 25일
try this:
B = arrayfun(@(x) repmat('#',1,x),A, 'UniformOutput', false)
B =
'####' [1x0 char]
'#' '########'

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by