MATLAB resulted MTX format - matlab

So.. I am writing this function in MATLAB but I don't like the result's format where MATLAB factors a constant out of the returned MTX.
The number I am looking for is 0.7584 as the second element in the returned MTX. However the result was displayed as
1.0e+04 * [2.0059 0.0001 0.0004].
I asked for the second element in the result and it was the value I wanted.
The question is: how can I make the function/MATLAB display the result as a MTX only without timing it with that constant.
any thoughts ?
Click here to see the screenshot
1

You can use format to set the output style of the command window.
>> a = rand(1, 3)*10e-4;
>> format short
>> disp(a)
1.0e-03 *
0.9649 0.1576 0.9706
>> format longe
>> disp(a)
9.648885351992765e-04 1.576130816775483e-04 9.705927817606157e-04
Try out the different options to find what suits you.

Related

rng('default') for rand() function in MATLAB vs Octave

I have a matlab piece of code that generates same random numbers (rand(n,1)) when I initialize with rng('default');
Example:
>> rng('default');
>> rand(3,1)
ans =
0.8147
0.9058
0.1270
Now I need to generate the same output in Octave. Is there an equivalent function for rng('default') in Octave? Please advise on how to get the same set of random numbers as MATLAB in Octave.
From the rand documentation for Octave
By default, the generator is initialized from /dev/urandom if it is available, otherwise from CPU time, wall clock time, and the current fraction of a second. Note that this differs from MATLAB, which always initializes the state to the same state at startup. To obtain behavior comparable to MATLAB, initialize with a deterministic state vector in Octave’s startup files (see ‘Startup Files’).
To get around this difference, you will have to seed both the MATLAB and Octave random number generator, and specify the generation method, to try and ensure they're doing the same thing. Note I say "try" because fundamentally they're different languages and there is no equivalency guarantee.
However, it appears that MATLAB and Octave don't use equivalent seeds. User Markuman has provided an example on the Octave wiki to get around this
Octave
function ret = twister_seed(SEED=0)
ret = uint32(zeros(625,1));
ret(1) = SEED;
for N = 1:623
## initialize_generator
# bit-xor (right shift by 30 bits)
uint64(1812433253)*uint64(bitxor(ret(N),bitshift(ret(N),-30)))+N; # has to be uint64, otherwise in 4th iteration hit maximum of uint32!
ret(N+1) = uint32(bitand(ans,uint64(intmax('uint32')))); # untempered numbers
endfor
ret(end) = 1;
endfunction
octave:1> rand('twister',twister_seed) # notice: default seed is 0 in the function
octave:2> rand
ans = 0.548813503927325
octave:3> rand
ans = 0.715189366372419
octave:4> rand
ans = 0.602763376071644
MATLAB
>> rand('twister',0)
>> rand
ans =
0.548813503927325
>> rand
ans =
0.715189366372419
>> rand
ans =
0.602763376071644
So from the documentation quote at the top of this answer, you could set the random number generator seed during Octave startup if you want this behaviour by default.

question about solving system of equations using symbolic math

I want to use symbolic symbol to solve a system of linear equation. So I prepare the following code.
A=[1,2;3,4];
% syms x
x=sym('x_%d',[2 1]);
eqn=A*x==[1;2];
result=solve(eqn,x)
Interestingly, it works, but when I read the variable result, it gives a 1X1 struct with x_1 and x_2 are 1X1 sym. But what I expect get should be 2 real values, why? Could someone explain it? Remark: do not want to use A^-1*[1;2] to obtain the answer.
If you set the output to single variable solve returns a structure
data type that contains all the solutions, to get each solution use
the dot. assignment, like result.x_1 or result.x_2
The code is as follows
A=[1,2;3,4];
% syms x
x=sym('x_%d',[2 1]);
eqn=A*x==[1;2];
result = solve(eqn,x);
result.x_1
% 0
result.x_2
% 1/2
If you want to have result as an array, use multiple output format, like
result(1) for the first variable, result(2) for the second variable
The code is as follows
A=[1,2;3,4];
% syms x
x=sym('x_%d',[2 1]);
eqn=A*x==[1;2];
[result(1), result(2)] = solve(eqn,x);
result
% result = [0 , 1/2]

the difference between makecform('srgb2xyz') and rgb2xyz() in matlab

I wonder what the matlab does when using the function rgb2xyz()?
I cannot re-produce the results using the rgb2xyz conversion matrix..
Moreover, is there any difference between using makecform('srgb2xyz') and using rgb2xyz()? they produce difference results..
The default white point for makecform('srgb2xyz') appears to be D50, whereas rgb2xyz defaults to D65.
>> applycform([.2 .3 .4],makecform('srgb2xyz','AdaptedWhitePoint',whitepoint('D65')))
ans =
0.0638 0.0690 0.1356
>> rgb2xyz([.2 .3 .4])
ans =
0.0638 0.0690 0.1356
>> applycform([.2 .3 .4],makecform('srgb2xyz'))
ans =
0.0617 0.0679 0.1024
>> rgb2xyz([.2 .3 .4],'WhitePoint','D50')
ans =
0.0616 0.0679 0.1025
Note the documentation for makecform suggests using the more recent rgb2xyz instead. As for your comment about reproducing the results using a matrix, note that the matrices are generally derived from / applied to linear data. If you want to reproduce the results you'll need to model the srgb gamma correction as well.

Matlab Double Precision Digits: Variable Editor vs. fprintf

Following this question, I was looking at the precision of double variables in Matlab. There, it is recommended to use fprintf to look at the variables more closely.
The strange thing is that the Variable Editor and fprintf show different results, fprintf shows one digit more.
% pi
Variable Editor: 3.141592653589793
fprintf('0.16f\n',pi): 3.1415926535897931
vpa('pi'): 3.1415926535897932384626433832795
% pi / 180
pi180 = pi/180
Variable Editor (pi180) 0.017453292519943
fprintf('%0.16f\n',pi180) 0.0174532925199433
vpa('pi/180') 0.017453292519943295769236907684886
Internally, Matlab seems to be working with the precision which is printed by fprintf
>> fprintf('%0.16f\n',0.0174532925199433*10) % value from fprintf
0.1745329251994330
>> fprintf('%0.16f\n',0.017453292519943*10) % value from Variable Editor
0.1745329251994300
>> fprintf('%0.16f\n',pi180*10) % internal calculation
0.1745329251994330
Why is that so?
If I use the precalculated pi/180 in a function, should I use the value from fprintf or from the Variable Editor?
tl;dr In the Variable Editor, Matlab is truncating at 15 digits instead of 16.
tl;dr Variables in MATLAB use double precision. The same precision is used for variables in both the Command Window and the Variable Editor. The only difference is the display format and the number of decimal places of accuracy you print it to.
MATLAB by default uses double precision for its variables. These variables in MATLAB are stored in the Workspace. Both the Command Window and the Variable Editor pull their variables from the same Workspace and hence underneath use the same precision.
The default format for displaying variables in the Variable Editor and the Command Window is the short format. You can check this for the Command Window with
>> get(0, 'format')
ans =
short
and for the Variable Editor by going to Preferences -> Variables -> Format. The short format by default displays variables to 4 decimal places of accuracy.
It appears to me that you have changed the default display format for variables in your Variable Editor to the long format. This format displays double variables with 15 decimal places of accuracy. Both the long and short formats round variables, so pi which is 3.14159 gets rounded to 3.1416 because of the 9 in the 5th decimal place when displayed with the short format
>> format short
>> pi
ans =
3.1416
This is directly equivalent to the output produced by fprintf
>> fprintf(1, '%.4f\n', pi);
3.1416
However, the long format, which I'm guessing, you've set as the default for your Variable Editor rounds to 15 decimal places and so displays
>> format long
>> pi
ans =
3.141592653589793
which is directly equivalent to
>> fprintf(1, '%.15f\n', pi);
3.141592653589793
When you use fprintf(1, '%.16f\n', pi); you are printing pi to 16 decimal places and not 15 as specified by the long format. This is why your output is
>> fprintf(1, '%.16f\n', pi);
3.1415926535897931
Note, the 1 at the end of this. This is why the 3 directly preceding it isn't rounded to 4 when displayed in your Variable Editor.
Summary
Variables in MATLAB by default use double precision
MATLAB variables are stored in the Workspace
Variables available in the Command Window and the Variable Editor both come from the Workspace and use the same precision
Precalculated Values
In MATLAB you should use the variable name pi180 in function calls or when manipulating other numeric data. This will use double precision and eliminate any copy and paste errors that may arise by using values output by fprintf or in the Variable Editor.
fprintf Quirks
tl;dr MATLAB's short and long formats switch between the %d, %.f, %.g and %.e specifiers depending on the most appropriate method for the input.
#horchler pointed out that %.f is only directly equivalent to the short and long formats for specific inputs and this is true. There is no direct equivalent for all inputs between fprintf and MATLAB's short and long formats.
For instance let's look at eps and 100.5 and try to print the numbers exactly like MATLAB's short and long formats.
>> format short
>> eps
ans =
2.2204e-16
>> 100.5
ans =
100.5000
and
>> format long
>> eps
ans =
2.220446049250313e-16
>> 100.5
ans =
1.005000000000000e+02
Now we know from above that fprintf(1, '%.4f\n', pi); and fprintf(1, '%.15f\n', pi); are directly equivalent to short and long respectively for pi but are they for eps and 100.5
>> fprintf(1, '%.4f\n', eps);
0.0000
>> fprintf(1, '%.15f\n', eps);
0.000000000000000
>> fprintf(1, '%.4f\n', 100.5);
100.5000
>> fprintf(1, '%.15f\n', 100.5);
100.500000000000000
No they aren't the only direct equivalent is fprintf(1, '%.4f\n', 100.5);. What if we try with %.g?
>> fprintf(1, '%.4g\n', eps);
2.22e-16
>> fprintf(1, '%.15g\n', eps);
2.22044604925031e-16
>> fprintf(1, '%.4g\n', 100.5);
100.5
>> fprintf(1, '%.15g\n', 100.5);
100.5
Now none of the fprintf statements are directly equivalent. However, we can produce a direct equivalent for eps using the long format with
>> fprintf(1, '%.16g\n', eps);
2.220446049250313e-16
because the number directly following the . for the %g format specifier specifies the number of significant digits (including those preceding the decimal point, .) we need to use 16 and not 15.
To produce a direct equivalent for all of these input types for the short format we need to mix the %.f, %.g and %.e specifiers as well as adjusting the field width.
>> format short
>> pi
ans =
3.1416
>> eps
ans =
2.2204e-16
>> 100.5
ans =
100.5000
>> fprintf(1, '%.4f\n', pi);
3.1416
>> fprintf(1, '%.5g\n', eps);
2.2204e-16
>> fprintf(1, '%.4f\n', 100.5);
100.5000
Not trivial at all. The same can be done for the long format.
>> format long
>> pi
ans =
3.141592653589793
>> eps
ans =
2.220446049250313e-16
>> 100.5
ans =
1.005000000000000e+02
>> fprintf(1, '%.15f\n', pi);
3.141592653589793
>> fprintf(1, '%.16g\n', eps);
2.220446049250313e-16
>> fprintf(1, '%.15e\n', 100.5);
1.005000000000000e+02
Even worse than for the short format.
So in short, MATLAB's short and long formats switch between the %d, %.f, %.g and %.e specifiers depending on the most appropriate method for the input.
Additional Reading
You can find information on the different display formats available in MATLAB through the format documentation. There is also a helpful document on Display Format for Numeric Values. And lastly, there is information about the Variable Editor and its preferences.

setting precision in legend notation in matlab

I have this:
leg2=strcat('Max Degree :',num2str(adet(1,1)/ch(l)));
leg3=strcat('Min Degree :',num2str(adet(1,2)/ch(l)));
leg4=strcat('Max Request :',num2str(adet(1,3)/ch(l)));
leg5=strcat('Min Request :',num2str(adet(1,4)/ch(l)));
leg6=strcat('Max Priority :',num2str(adet(1,5)/ch(l)));
leg7=strcat('Random :',num2str(adet(1,6)/ch(l)));
leg8=strcat('AICS :',num2str(adet(1,7)/ch(l)));
legend(leg2,leg3,leg4,leg5,leg6,leg7,leg8,'Location','SouthWest');
Here, adet(1,1)/ch(l) is a number between 0 and 1. And sometimes it becomes something like 0.01666667. I'd like to set a precision for it to make it something like "0.02".
I can do it with disp() function with the format bank. But I don't know how to apply it in a plot. and sorry for bad english.
Use a format specifier in num2str (within the legend statement):
>> num2str(pi) %// default
ans =
3.1416
>> num2str(pi, '%.2f') %// two decimal figures
ans =
3.14