View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Mat2gray variant with dimension option

Be the first to rate this file! 4 Downloads (last 30 days) File Size: 2.54 KB File ID: #39162 Version: 1.4
image thumbnail

Mat2gray variant with dimension option


Jurgen (view profile)


27 Nov 2012 (Updated )

Normalizes sections of an N-D matrix divided along DIM to the 0.0-1.0 range.

| Watch this File

File Information

Similar to mat2gray. Except dim2gray allows user to specify a dimension DIM that will determine the limits used to normalize values in that dimension.
In contrast, mat2gray scales all values in A based on 2 values (min and max by default).

I found it useful for normalizing RGB/HSV images per color channel in 1 short command, but tried to make it so it accepts N-D matrices.

from the help:
DIM2GRAY - Normalize N-dimensional array along a specified dimension.
 Normalizes (i.e. scales values) in matrix A along the dimension DIM
 B has type double or single in the 0-1 range, and is the same size as A.
 DIM is an integer scalar--A is 'sliced' along DIM.
 LIMS, optional, is a Nx2 vector of limits--where N == size(A,DIM).
 It specifies limits per dimensional slice, similar to mat2gray(A,LIMS).
 Usage without LIMS defaults to min-max, like mat2gray(A), only
 dim2gray uses min-max limits per slice/chunk. Use NaN to specify a
 min or max limit.

 Example: an HSV image
 A = imread('peppers.png');
 A = rgb2hsv(A);
 B = dim2gray(A,3)

 Will linearly normalize every color plane to 0.0 & 1.0 based on the min
 and max *per channel*. Equivalent to:
 B = cat(3,mat2gray(A(:,:,1)),mat2gray(A(:,:,2)),mat2gray(A(:,:,3)));

 %This clips range at .1 and .9:
 C = dim2gray(A,3,[0.1 0.9;0.1 0.9;0.1 0.9])

 Example: Multiple measurements of signal(e.g. voltage) over time
 Let every row be a measurement(5) and every column a timepoint(32).
 DATA = rand(5,32);
 dim2gray(DATA,2); %normalize every measurement to 0-1 range
 dim2gray(DATA,1); %normalize every unique timepoint to 0-1 range

Hope it helps. Welcome feedback.

Required Products Image Processing Toolbox
MATLAB release MATLAB 7.6 (R2008a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (2)
22 May 2013 Jurgen

Jurgen (view profile)

Hi Lili, I don't think I understand. Can you give an example of what you mean?

Comment only
22 May 2013 Lili Johnson


It is a very useful app. I want to know how I can save each splitted images individually.

Best Regards,

Comment only
12 Feb 2013 1.1

Cleaned up code & comments. Added LIMS option.

12 Feb 2013 1.2


12 Feb 2013 1.3

Something went wrong with previous edit, no change. New attemp.

13 Feb 2013 1.4

Corrected bug in NaN handling for LIMS.

Contact us