Filter löschen
Filter löschen

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 Ansichten (letzte 30 Tage)
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 Kommentare
the cyclist
the cyclist am 18 Feb. 2018
Bearbeitet: the cyclist am 18 Feb. 2018
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?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

the cyclist
the cyclist am 18 Feb. 2018
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.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by