I have a string array ["Black" "Brown" "Red" "Orange" "Yellow" "Green" "Blue"] and I want to replace the colors with values 0, 0 0, 0 0 0, ... until i get to 0 0 0 0 0 0 0.

4 次查看(过去 30 天)
Array should look like [0, 0 0, 0 0 0, 0 0 0 0, 0 0 0 0 0, 0 0 0 0 0 0, 0 0 0 0 0 0 0] instead of the original one containing colors.I am unsure how to do it.
  2 个评论
the cyclist
the cyclist 2018-2-18
编辑:the cyclist 2018-2-18
Can you add a little more detail? This is not Twitter, so please make it easy on us.
For example, is the output a numeric matrix, like
[0 0 0 0 0 ...]
? Or is it a string matrix, such as ...
out = ["0" "0 0" "0 0 0"]
Also, does it matter if a color is repeated?

请先登录,再进行评论。

回答(1 个)

the cyclist
the cyclist 2018-2-18
Here is one way:
s = ["Black" "Brown" "Red" "Orange" "Yellow" "Green" "Blue"];
L = length(s);
Z = strings(1,7);
Z(1) = "0";
for ns = 2:L
Z(ns) = strcat(Z(ns-1)," 0");
end
I don't have much experience working with strings, so I expect there is a more efficient way.

类别

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