Created
June 12, 2019 12:08
-
-
Save robertoostenveld/51e2d9f477863a9fd4f43b3b0ed47404 to your computer and use it in GitHub Desktop.
MATLAB code that determines the scaling factor to convert an SI unit from one to another prefix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function scale = prefixscale(old, new) | |
% PREFIXSCALE determines the scaling factor to convert an SI unit from one to another prefix | |
% | |
% For example | |
% prefixscale('kg', 'mg') | |
% returns 1000000. | |
% Copyright (C) 2019, Robert Oostenveld | |
prefix = { | |
'Y' | |
'Z' | |
'E' | |
'P' | |
'T' | |
'G' | |
'M' | |
'k' | |
'h' | |
'da' | |
'_' | |
'd' | |
'c' | |
'm' | |
'u' | |
'n' | |
'p' | |
'f' | |
'a' | |
'z' | |
'y' | |
}; | |
multiplier = [ | |
1e24 | |
1e21 | |
1e18 | |
1e15 | |
1e12 | |
1e9 | |
1e6 | |
1e3 | |
1e2 | |
1e1 | |
1e0 | |
1e-1 | |
1e-2 | |
1e-3 | |
1e-6 | |
1e-9 | |
1e-12 | |
1e-15 | |
1e-18 | |
1e-21 | |
1e-24 | |
]; | |
oldindex = find(strcmp(prefix, '_')); | |
newindex = find(strcmp(prefix, '_')); | |
for i=1:numel(prefix) | |
if startsWith(old, prefix{i}) && ~isequal(old, prefix{i}) | |
oldindex = i; | |
end | |
if startsWith(new, prefix{i}) && ~isequal(new, prefix{i}) | |
newindex = i; | |
end | |
end | |
scale = multiplier(oldindex) ./ multiplier(newindex); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment