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

1 次查看(过去 30 天)
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?

采纳的回答

Bruno Luong
Bruno Luong 2018-9-21
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])

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by