Azzera filtri
Azzera filtri

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 visualizzazioni (ultimi 30 giorni)
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 Commenti
the cyclist
the cyclist il 18 Feb 2018
Modificato: the cyclist il 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?

Accedi per commentare.

Risposte (1)

the cyclist
the cyclist il 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.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by