Effacer les filtres
Effacer les filtres

Converting a numeric array into a string array with their corresponding values

1 vue (au cours des 30 derniers jours)
I am trying to convert a user inputted resistance value to its appropriate color band.
The user is told to input a value for resistance in the form of a vector.
If, for example, the user inputs [1 0 0 0], How do I output [Brown, Black, Red] as the appropriate color band sequence using conditional statements?
When converting resistance to a color band, the following should be considered:
BLACK(0) BROWN(1) RED(2) ORANGE(3) YELLOW(4) GREEN(5) BLUE(6) VIOLET(7) GREY(8) WHITE(9)
The first and second colors in the color band are determined by their associated number.
The third, final, color in the color band is determined by the following:
black(1) brown(10) red(100) orange(1000) yellow(10000) green(100000) blue (1000000)
For example, if the user enters [2 7 0 0 0]
2 corresponds with red, 7 corresponds with violet and there are three zeros so the last color is orange
[red violet orange]
With those rules in mind, how do I complete this task?
  1 commentaire
Stephen23
Stephen23 le 21 Sep 2018
"With those rules in mind, how do I complete this task?"
With indexing.

Connectez-vous pour commenter.

Réponse acceptée

Bruno Luong
Bruno Luong le 21 Sep 2018
cl = {'BLACK' 'BROWN' 'RED' 'ORANGE' 'YELLOW' 'GREEN' 'BLUE' 'VIOLET' 'GREY' 'WHITE'};
%input = [2 7 0 0 0];
input=[1 0 0 0]
s = cl([input(1:2)+1, length(input)-1])

Plus de réponses (1)

Walter Roberson
Walter Roberson le 21 Sep 2018
Hint:
lookup = {'PIE', 'TART', 'PUDDING', 'CAKE'};
lookup{User_value+1}

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by