MATLAB: I’m trying to create a 3D RGB matrix without loops.

3d matrixmatrix

Like I said, I'm trying to create a 3D matrix with 3 layers (RGB) without loops. The dimensions are 256 x 256 x 3. The Red layer is 0 along the top and increases to 255 at the bottom. The Green layer is 0 along the left side and increases to 255 on the right side. The Blue layer is 255 along the left side and decreases to 0 on the right side. How should I go about this?

Best Answer

Try this:
ramp = uint8(0:255);
redChannel = repmat(ramp', [1, 256]);
greenChannel = repmat(ramp, [256, 1]);
blueChannel = repmat(255-ramp, [256, 1]);
% Recombine separate color channels into a single, true color RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
imshow(rgbImage);
axis on;
Related Question