How to check if matrix values are in set parameters

3 views (last 30 days)
Conner Carriere
Conner Carriere on 7 Feb 2021
Answered: Ritvik Garg on 15 Jun 2021
I need to check to see if a color falls in between parameters i set
For example;
Mr = [232 12 31];
I need to see if Mr falls between the set values in these matracies
Red = [212 0 0; 255 34 255];
Orange = [240 119 0; 255 146 255];
Blue = [0 0 229; 2 73 255];
Green = [0 231 57; 2 255 118];
White = [216 229 233; 255 255 255];
Yellow = [215 230 0; 255 255 34];
After it passes the check that it is in red, and no other color, I need it to turn to the simplest red possible in matlab syntax, so,
Mr = [255,0,0]/255
Mr = [1 0 0]
I think then, i should be able to call it red in matlab like this, 'r' or 'red'
thank you, any info helps

Answers (1)

Ritvik Garg
Ritvik Garg on 15 Jun 2021
Hi Conner,
You can loop over the given matrices and check if Mr falls between the set values in each matrix.
Here's an example code which does this :
rgb_colors = [Blue;Green;Orange;Red;White;Yellow]; % creating a single 2-D matrix of size 12x3
color_code = ['b';'g';'o';'r';'w';'y']; % color's short code in the same order as above matrix
for i=1:2:12
if Mr(1) >= rgb_colors(i,1) & Mr(1)<=rgb_colors(i+1,1) & Mr(2) >= rgb_colors(i,2) & Mr(2)<=rgb_colors(i+1,2) & Mr(3) >= rgb_colors(i,3) & Mr(3)<=rgb_colors(i+1,3)
color_index = (i+1)/2;
break;
end;
end;
Mr = color_code(color_index); % use this color code wherever you want
Also check out this documentation page for more information on Color Specification and eight predefined colors.
Hope this helps..!!

Community Treasure Hunt

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

Start Hunting!