Change decimal numbers in variable editor in MATLAB - matlab

Suppose that we have this code:
a = pi;
b = pi+2;
c = pi*2.2
out = [a b c]
returns:
out =
3.1416 5.1416 6.9115
I want this output (without rounding values to two decimal):
out =
3.14 5.14 6.91
I don't want print these values. I want see them with 2 decimals here:

Use format bank to get 2 decimal displayed. More details about the format function in the documentation.
If you want to change how the variables are displayed in the variable editor, have a look at this page of the documentation.

In case you just want to use the first 2 decimals, you can add this line to your previous code:
out(:) = str2num(sprintf('%6.2f',out(:))); % 2 = number of decimals
This is not a beautiful solution but it truncates your values to the 2nd decimal and stores it erasing the following decimals. You will still have some zeros at the end (till you fill the selected format for your variable editor as explained above).

Related

How to split cell array values into two columns in MATLAB?

I have data in a cell array as shown in the variable viewer here:
{[2.13949546690144;56.9515770543056],
[1.98550875192835;50.4110852121618],
...}
I want to split it into two columns with two decimal-point numbers as:
2.13 56.95
1.98 50.41
by removing opening and closing braces and semicolons such as [;]
(to do as like "Text to columns" in Excel).
If your N-element cell array C has 2-by-1 numeric data in each cell, you can easily convert that into an N-by-2 numeric matrix M like so (using the round function to round each element to 2 significant digits):
M = round([C{:}].', 2);
The syntax C{:} creates a comma-separated list of the contents of C, equivalent to C{1}, C{2}, ... C{N}. These are all horizontally concatenated using [ ... ], then the result is transposed using .'.
% let's build a matching example...
c = cell(2,1);
c{1} = [2.13949546690144; 56.9515770543056];
c{2} = [1.98550875192835; 50.4110852121618];
% convert your cell array to a double array...
m = cell2mat(c);
% take the odd rows and place them to the left
% take the even rows and place them to the right
m = [m(1:2:end,:) m(2:2:end,:)];
% round the whole matrix to two decimal digits
m = round(m,2);
Depending on your environment settings, you may still see a lot of trailing zeros after the first two decimal digits... but don't worry, everything is ok (on the precision point of view). If you want to display only the "real" digits of your numbers, use this command:
format short g;
you should use cell2mat
A={2.14,1.99;56.95,50.41};
B=cell2mat(A);
As for the rounding, you can do:
B=round(100*B)/100;

MATLAB is rounding my values to 1

n = 215;
N = 215.01:0.1:250;
p = 0.52;
q = 0.48;
Gamblers = (1 - (q/p)^n)./(1 - (q/p).^N);
plot(Gamblers)
Matlab takes the numerators and denominators as simply 1, filling the array with nothing but that value. How can I correct this?
Your denominator and numerator are very close to 1 but not exactly 1. The plot(Gamblers) confirms this.
By default MATLAB will display numbers with four digits after the decimal place. Your numerator is 0.999999966414861, which with four digits rounds to 1. MATLAB uses double precision numbers so your calculation here is still accurate.
Try double clicking on the Gamblers variable to open the variables window and then double clicking on one of the results. You'll see it change from the default display precision to a much more accurate depiction of your variable.

Displaying big doubles in matlab

I have a vector of doubles and I want to see what are the exact numbers inside the vector I get in format long.
1.0e+03 *
-0.002202883146567
1.182072110137121
-0.002242966651629
-0.000584787748712
0.022251505213305
0.037460846794487
Can I make some adjusment so that I can directly see the number, rounded to let's say the 5th or 6th element after the decimal point, whenever I type the name of the variable?
fprintf('%.6f\n', 0.037460846794487)
It'll round 0.037460846794487 to 6 decimal places as shown:
>> fprintf('%.6f\n', 0.037460846794487)
0.037461
Or you can also use sprintf('%.6f\n', 0.037460846794487) , particularly, if you want to save the rounded off output in a variable.
>> a=sprintf('%.6f\n', 0.037460846794487)
a =
0.037461
and for the matrix you mentioned, you can make the following adjustment:
%Your matrix
A = 1.0e+03 * [ -0.002202883146567 ;
1.182072110137121 ;
-0.002242966651629 ;
-0.000584787748712 ;
0.022251505213305 ;
0.037460846794487 ];
A = sprintf('%.6f\n', A) %Adjusted to 6 decimal digits

Extracting digits after decimal MATLAB

I am representing a chaotic number with 15 digits after decimal i.e format long in matlab and i want to extract 3 numbers each containing 5 digits from the chaotic number. i.e if chaotic no. is 0.d1d2...d15 then i want (d1..d5),(d6..d10) & (d11..d15). it is always rounding off.I have written the following code.
num=x(n);
a1=floor(num*10^5);
disp('a1');
disp(a1);
a2=floor(num*10^10-a1*10^5);
disp(a2);
num=floor(num*10^15);
a3=mod(num,100000);
disp('a3');
disp(a3);
and the output is
0.320446597556797
a1
32044
a2
65975
a3
56796
its showing a3 as 56796 but i want 56797.
please help!!!
The problem is, very likely, that each operation you do to extract a group of digits introduces a small error, which is reflected in the last digit.
An alternative approach that avoids this:
num = pi; %// for example
str = num2str(num,'%0.15f'); %// string representation with 15 decimal digits
str = str(find(str=='.')+1:end); %// keep only digits after decimal point
a = base2dec(reshape(str,5,[]).',10); %'// convert string parts into numbers
Try this approach using strings -
n = 0.320446597556797 %// Input decimal number
t1 = num2str(n,'%1.15f')
t2 = strfind(t1,'.')
a = str2num(reshape(t1(t2+1:end),5,[])') %// output as a single matrix
Thanks to #Luis for the reshape idea! Hope you won't mind Luis!
It is probably a rounding error but if you really want those specific values, simply cast to a string and then rip the string apart. Something simple like below
a = 0.320446597556797;
format longg
aStr = num2str(a,'%1.15f');
decimals = regexp(aStr,'\.','Split');
decimals = decimals{2};
a1 = str2double(decimals(1:5));
a2 = str2double(decimals(6:10));
a3 = str2double(decimals(11:15));

Using matlab,how to find the last two digits of a decimal number?

How can one find the last two digits of a decimal number using MATLAB?
Example:
59 for 1.23000659
35 for 56368.35
12 for 548695412
There will always be issues when you have a decimal number with many integer digits and fractional digits. In this case, the number of integer and decimal digits decide if we are correct or not in estimating the last two digits. Let's take at the code and the comments thereafter.
Code
%// num is the input decimal number
t1 = num2str(num,'%1.15e') %// Convert number to exponential notation
t1 = t1(1:strfind(t1,'e')-1)
lastind = find(t1-'0',1,'last')
out = str2num(t1(lastind-1:lastind)) %// desired output
Results and Conclusions
For num = 1.23000659, it prints the output as 59, which is correct thanks
to the fact that the number of integer and decimal digits don't add upto
more than 16.
For num = 56368.35, we get output as 35, which is correct again and the
reason is the same as before.
For num = 548695412, we are getting the correct output of 12 because of the
same good reason.
For an out of the question sample of num = 2736232.3927327329236576
(deliberately chosen a number with many integer and fractional digits),
the code run gives output as 33 which is wrong and the reason could be
inferred from the fact that integer and decimal digits add upto a much
bigger number than the code could handle.
One can look into MATLAB command vpa for getting more precision, if extreme cases like the 4th one are to dealt with.
Convert to string and then extract the last two characters:
x = 1.23; % x = 1.23
s = num2str(x); % s = "1.23"
t = s(end-1:end); % t = "23"
u = str2num(t); % u = 23
Note: depending on your specific needs you might want to supply a precision or formatSpec to num2str.
The other two answers are nice and straight forward, but here you have a mathematical way of doing it ;)
Assuming a as your number,
ashift=0.01*a; %shift the last two digits
afloor=floor(ashift); %crop the last two digits
LastDecimals=a-100*afloor; %substract the cropped number form the original, only the last two digits left.
Of course if you have non-natural numbers, you can figure those out too with the same "floor and subtract technique as above.