If you want the community to understand your code and help you, you may need to provide bit more details about your code and your use case. For example, in this case, I am not really sure what are the variables y, im and bw represent. I am not really sure about whether you want to find the mean intensity in the ROI or you want to find all the unique intensities in the ROI. What do you mean by changing colors based on intensity?
If you are using a grayscale image im and based on the pixel value you want to assign a certain color at these locations, you may probably want to use find command which finds the locations of a specific intensity k and you can assign any specific color (r,g,b) to that locations.
im_new(idx,idy,:)=[ r, g ,b];