Given a positive number , how to determine its surrounding integers in matlab - matlab

Can someone please explain how to get the surrounding integers for a given positive number ( for ex. if number is 18.2378 then it should return 18 and 19 )
(I actually need this to determine that the given number is between 0-1 or 2-3 or 4-5 and so on....and if it is in between 0-1 or 2-3 or 4-5 etc then some expression evaluates , else some other expression must evaluate.)

The floor and ceil functions do this:
x = 18.2378;
floor(x); %Returns 18
ceil(x); %Returns 19

floor(18.2378) will return 18 i.e. the previous nearest integer.
ceil(18.2378) will return 19 i.e. the next nearest integer

You can use round or floor or ceil in Matlab to turn decimal numbers into integers. Round will round up or down depending on the decimal value, floor rounds toward minus infinity, and ceil rounds toward positive infinity.
Here is an example of how this could work:
n=18.2378;
F=floor(n);
C=ceil(n);
TF=F<n<C;
F returns 18. C returns 19. TF will return 1 if the number is between the floor and the ceiling--But, if you do it this way the number will always be between its floor and ceiling--and 0 if it is not. You can do this iteratively in a loop as many times as you need.

Related

Dart: What is the difference between floor() and toInt()

I want to truncate all decimals of a double without rounding. I have two possibilities here:
double x = 13.5;
int x1 = x.toInt(); // x1 = 13
int x2 = x.floor(); // x2 = 13
Is there any difference between those two approaches?
As explained by the documentation:
floor:
Rounds fractional values towards negative infinity.
toInt:
Equivalent to truncate.
truncate:
Rounds fractional values towards zero.
So floor rounds toward negative infinity, but toInt/truncate round toward zero. For positive values, this doesn't matter, but for negative fractional values, floor will return a number less than the original, whereas toInt/truncate will return a greater number.

About Random Number Generator in Matlab

The rand function in matlab generates a random number between 0-1 with space 0.0001. Is there a way to widen this space so that the number generated is only to the first or second decimal place?? Thanks for any suggestion.
You can generate pseudo-random integers between [0,10^n] using randi and divide by 10^n:
randTens = randi([0,10 ])/10;
randHundreds = randi([0,100])/100;
...
You can use round function:http://www.mathworks.com/help/matlab/ref/round.html
Y = round(X,2)
Y = round(X,N) rounds to N digits:
N > 0: round to N digits to the right of the decimal point.
N = 0: round to the nearest integer.
N < 0: round to N digits to the left of the decimal point.
I understand Matlab generates uniformly spaced random numbers. If you want to widen the precision step, you could try multiplying rand by some value 'a' giving you 0.0001*a spacing and then selecting the numbers that are inferior to 1.

What is the meaning of number 1e5?

I have seen in some codes that people define a variable and assign values like 1e-8 or 1e5.
for example
const int MAXN = 1e5 + 123;
What are these numbers? I couldn't find any thing on the web...
1e5 is a number expressed using scientific notation and it means 1 multiplied by 10 to the 5th power (the e meaning 'exponent')
so 1e5 equals 1*100000and is equal to 100000, the three notations are interchangeable meaning the same.
1e5 means 1 × 105.
Similarly, 12.34e-9 means 12.34 × 10−9.
Generally, AeB means A × 10B.
this is scientific notation for 10^5 = 100000
1e5 is 100000. 5 stand for the amount of zeros you add in behind that number. For example, lets say I have 1e7. I would put 7 zeros behind 1 so it will become 10,000,000. But lets say that the number is 1.234e6. You would still add 6 zeros at the end of the number so it's 1.234000000, but since there is that decimal, you would have to move it to the right 6 times since it's e6.
The values like:
1e-8 or 1e5
means;
1e-8 = 1 * 10^(-8)
And
1e5 = 1 * 10

rounding down in TSQL 2005

Is there a simply way to convert the following Excel formula into TSQL:
ROUNDDOWN((table.OrderQty / table.PacksPerCase),0)
table.OrderQty = 9
table.PacksPerCase = 5
"0" indicate that the result will have zero decimal places
Result = 1.0
table.OrderQty = 9
table.PacksPerCase = 4
"0" indicate that the result will have zero decimal places
Result = 2.0
Try using FLOOR :
The FLOOR function returns the largest integer less than or equal to
the specified numeric expression.
Your expression should be:
FLOOR(table.OrderQty / table.PacksPerCase)
ROUNDDOWN() rounds towards zero for negative numbers.
Since FLOOR rounds towards negative infinity, the closest equivalent in TSQL I can think of is ROUND with function set to 1;
ROUND((table.OrderQty / table.PacksPerCase), 0, 1)
If you're only dealing with positive numbers, of course FLOOR will also work well.

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.