Matlab number formatting? - matlab

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.

Related

how to retain the decimal part in redshift when the decimal part is only having 0

In my code we are using a multiplication as (180.00*60.00).
Ideally since both the numbers are of decimal type I'm expecting my answer to be also in decimal. But in redshift I observed that this equation is resulting only to Integer.
Basically when this multiplication happens I want the answer as 10800.0000 and not 10800.
how to achieve this in Redshift. I tried putting the cast to whole expression and also even to individual expression but still I'm getting the answer only in Integer.
any help here?
If I understand your question correctly you are asking why Redshift isn't inferring that the string "180.00*60.00" as being the multiplication of 2 values in decimal type, yes? If correct you just need to cast the input values for the multiplication. For example:
select 180.00::decimal(6,2) * 60.00::decimal(6,2) ;
It also looks like you want the result to have 4 significant digits so you may want to cast to decimal(8,4).
If I've missed the point of your question please clarify.

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

How to multiply numbers in Swift?

I am working on a calculator in Swift, but I have a small problem:
when multiplying two numbers, I don't have the same results as a regular calculator.
For example:
In Swift :
0.333328247070312 * 16 = 5.33325195312499
In a regular calculator:
0.333328247070312 * 16 = 5.333251953
What should I do to get the same results as a regular calculator in Swift?
Your "regular calculator" seems wrong or weird in result printing, so, you should not rely on it. I've rechecked your calculation in Python3 which is known to calculate all in binary64 double and print the most exact decimal form:
>>> 0.333328247070312 * 16
5.333251953124992
it's even more detailed (by one digit) than Swift output. Your output also can't be verified as binary32 calculation, because the latter has ~7 correct decimal digits, and usually isn't printed with more digits. What this calculator is? I'd suppose some Pascal-based tool due to its custom 6-byte float.
Try to ask your calculator to print the most detailed form. If it fails, throw it away and use a most exact tool to verify, or, if your task is really to get the same result, figure out more details about its processing.

Random Number in Octave

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

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