Create a string array (MxN) where each element is a repeated character based off a numeric array (MxN)
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Carl
el 25 de Nov. de 2019
Comentada: Carl
el 25 de Nov. de 2019
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
0 comentarios
Respuesta aceptada
Bhaskar R
el 25 de Nov. de 2019
B = string(cellfun(@(x)repelem('#', x), num2cell(A), 'UniformOutput', false));
3 comentarios
Bhaskar R
el 25 de Nov. de 2019
Hahaha, I think your version is less tha R2016b. Data type String is introduced in R2016b.
Anyway your answer is more suitable than mine, Carl asked for string matrix output, pass your answer to string thats it. :-)
B = string(arrayfun(@(x) repmat('#',1,x),A, 'UniformOutput', false))
Más respuestas (1)
Philippe Lebel
el 25 de Nov. de 2019
try this:
B = arrayfun(@(x) repmat('#',1,x),A, 'UniformOutput', false)
B =
'####' [1x0 char]
'#' '########'
0 comentarios
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!