Rounding to Order of Magnitude in Matlab

Knowing what order of magnitude a number is can be really helpful when performing analysis.  I find this especially useful when setting my color-scale using caxis.

To find the order of magnitude of a number, denoted as O(10n) of some number A in matlab, use:

n=floor( log10(A));

Example: Say I want to plot this constrained to some integer value of 10^n.  See code attached
Example: Say I want to plot this constrained to some integer value of 10^n. See code attached

A few examples:

A=.00314;  n=-3; 10n=.001;

A=.95;  n=-1; 10n=.1;

A=6; n=0; 10n=1;

A=12456789; n=7; 10n=10000000;

And here's an example of when I might use this. Say I don't know How big the values in the matrix C1C2 are going to be, but they could vary orders of magnitude between experiments.

 Cmax=10^(ceil( log10( max(C1C2(:)) )));
    imagesc(Xs,Ys,C1C2);caxis([0 Cmax])
    colormap(flipud(gray));
    cbfreeze(colorbar('location','EastOutside',...
             'YTick',...
             [0,Cmax/2,Cmax],...
             'YTickLabel',...
            {0,Cmax/2,Cmax }));
    freezeColors;

For more on the specific options i used in making this plot, see my post about Making Pretty Matlab Plots

  • filippo

    hi Mike,
    I was looking for a neat way to get the order of magnitude of a number, and so I landed here on your website.
    Your proposed solution is very nice, I just wanted to point out that you might want to add an "abs(...)" to avoid passing a negative number to the log10 function, which will result in an imaginary number output.

    just my 2 cents.

    See you 🙂