Random Number in Octave - matlab

I need to generate a random number that is between .0000001 and 1, I have been using rand(1) but this only gives me 4 decimal points, is there any other way to do this generation?
Thanks!

From the Octave docs:
By default, Octave displays 5 significant digits in a human readable form (option ‘short’ paired with ‘loose’ format for matrices).
So it's probably an issue with the way you're printing the value rather than the value itself.
That same page shows the other output formats in addition to short, the one you may want to look in to is long, giving 15 significant digits.
And there is also the output_precision which can be set as per here:
old_val = output_precision (7)
disp (whatever)
old_val = output_precision (old_val)

Set the output_precision to 7 and it should be ok :)

Setting the output precision won't help though because the number can still be less than .0000001 in theory but you will only be displaying the first 7 digits. The simplest way is:
req=0;
while (req<.0000001)
req=rand(1);
end
It is possible that this could get you stuck in a loop but it will produce the right number. To display all the decimals you can also use the following command:
format long
This will show you 15 decimal places. To switch back go:
formay short

Related

printmat function: Decimal and percentage

I am very new to MATLAB. I am sorry if my question is basic. I am using "printmat" function to show some matrices in the command console. For example, printmat(A) and printmat(B), where A = 2.79 and B = 0.45e-7 is a scalar (for the sake of simplicity).
How do I increase the precision arbitrarily to seven decimals? For example: my output looks like 2.7943234 and B = 0.00000004563432.
How do I add a currency (say dollar) figure to the output of printmat?
How do I add a percentage figure (%) to the output of printmat?
Note: The reason I use printmat is that I can name my rows and columns. If you know a better function that can do all above, I would be glad to know.
Regards Mariam. From what I understand, you would like to display the numbers and show their full precision. I am also newbie, If I may contribute, you could convert the number data to string data (for display purposes) by using the sprintf function.
I am using the variable A=2.7943234 as example. This value will not display the full precision, instead it will display 2.7943. To show all the decimal tails, you could first convert this to string by
a = sprintf('%0.8f',A);
It will set the value a to a string '2.79432340'. The %0.8f means you want it to display 8 decimal tails. For this example,%0.7f is sufficient of course.
Another example: A=0.00000004563432, use %0.14f.
A=0.00000004563432;
a=sprintf('%0.14f $ or %%',A);
the output should be : '0.00000004563432 $ or %'.
You could analyze further in https://www.mathworks.com/help/matlab/ref/sprintf.html
You could try this first. If this does not help to reach your objective, I appreciate some inputs. Thanks.
The printmat function is very obsolete now. I think table objects are its intended successor (and functions such as array2table to convert a matrix to a table of data). Tables allow you to add row and column names and format the columns in different ways. I don't think there's a way to add $ or % to each number, but you can specify the units of each column.
In general, you can also format the display precision using format. Something like this may be what you want:
format long

Number of decimal digits to show

How to change the number of decimal digits?
Changing the format Matlab can show only 4 (if short) or 15 (if long). But I want exactly 3 digits to show.
To elaborate on Hamataro's answer, you could also use roundn function to round to a specific decimal precision, e.g.: roundn(1.23456789,-3) will yield 1.235. However, Matlab will still display the result in either of the formats you have mentioned, i.e 1.2350 if format is set to short, and 1.235000000000000 if format is set for long.
Alternatively, if you use sprintf, you can use the %g formatting option to display only a set number of digits, regardless of where the decimal point is. sprintf('%0.3g',1.23456789) yields 1.23; sprintf('%0.3g',12.3456789) yields 12.3
You can either use sprintf or do *
var2 = round(var1*1000)/1000

MATLAB UITABLE AND FORMAT OPTIONS

I have followed many posts about format of numbers in Matlab. Now I have set up Matlab uitable in such a way that one can fill the table cell by cell. I want the first column to maintain numbers up to say 12 digits, so I set the format to longG. Matlab uitable is refusing to accept my format and the frustrating part is, in the command window all digits are shown even up to 15 digits with format longG option. This is the sample;
colNam={'W','X','Y','Z'};
t=uitable('ColumnName',colNam,'Data',zeros(4),'ColumnEditable',...
[true true true true],'ColumnWidth',{150,'auto'},...
'ColumnFormat',{'numeric'});
set(t,'ColumnFormat',{'longG'});
I know of sprintf and fprintf as has been explained elsewhere. But please what we are refusing to realize is that, these change the class of the number from double to char. What if you want to set up the table so that it increases by 1 dynamically (credits to thewaywewalk)? We can't add double to char for this purpose. Please this is frustrating. Any suggestions? Thank you.
There is no such ColumnFormat property called "longG", where dis you see that ?
You have the possibility of manipulating the Java part of the Uitable in order to really display numeric values... but I think the best (and easiest) way is to use char.
If you want to increase the values, just convert it first to numeric, increase, then convert it back.
Based on John's suggestion, I put up this code and I could set the table to accept any number of digits.
f=figure;
dat=[101100220001;101100220002;101100220003;101100220004;101100220005];
colNam={'W'};
t=uitable('ColumnName',colNam,'Data',dat,'ColumnEditable',...
[true],'ColumnWidth',{150},'pos',[5 250 200 120]);
newdat=get(t,'Data');
newdat=strtrim(cellstr(num2str(newdat))); %Convert data to cell array of strings
tt=uitable('ColumnName',colNam,'Data',newdat,'pos',[300 250 200 120]);

Matlab number formatting?

I'm having some difficulties processing some numbers. The results I get are some like:
0.000093145+1.6437e-011i
0.00009235+4.5068e-009i
I've already try to use format long and as alternative passing to string and then str2num and with no good results also. Although is not being possible to convert them properly as I want (e.g. to a number with 9 decimals) If nobody is able to help me, at least I would appreciate if someone can tell me how to interpret the meaning of the i base.
You are talking about the imaginary unit i. If you are just using real number, you could neglect the imaginary part (it is very small). Thus, try:
real(0.000093145+1.6437e-011i)
After taking real() you can also control the decimal place formatting by sprintf:
sprintf('%0.2f', pi)
Will result in:
'3.14'
Place a 9 instead of a 2 for 9 decimal places.

How to change the number of significant figures in a Matlab tree

I have made a regression tree, but Matlab shows numbers with 6 significant figures. The tree is very dense, and I don't need such a high precision, which makes it hard to read.
How can I change it to 3 figures for example?
you can use vpa
e.g.
vpa([114.234 0.0013452],3)= [ 114.0, 0.00135]
I think you need the following
while printing something you may use
a=2.335444444444
sprintf ('%.2f',a) % I want to print 2 significant digits of a only
Output
ans =
2.34
Try changing the format to something of your taste (help format)
You may be interested in:
format bank
or
format short