Matlab increment bug? [duplicate] - matlab

This question already has answers here:
Matlab gives wrong answer
(3 answers)
Closed 8 years ago.
I don't know what's going on but when I use i = 0.1:0.1:7 in my code I get the integers 1 and 2, it then skips 3 but gets 4, 5, 6 and 7 no problems.
x=zeros((t_f/h)+1,1);
x(1,1)=0;
table=zeros(t_f,1);
for i=0.1:0.1:7;
x(round(i/h)+1,1)=i;
if ~mod(i,1)
table(i,1)=i;
end
end
Then to test I returned these
table
a=[x(1) x(11) x(21) x(41) x(51) x(61) x(71)]
a=[x(1) x(11) x(21) x(31) x(41) x(51) x(61) x(71)]
It's not finding 3 as an integer because i never is 3, it's 3.000... but 1, 2, 4, 5, 6 and 7 are integers.

It a floating point number representation issue. When the numbers are constructed in this way, the number you expect to be exactly 3 is off by a small amount (4.440892e-16). This is expected behaviour, not a bug, see: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

Related

How is this MATLAB code (involving colon operator) resolved?

Recently, I wanted to calculate the next multiple of 5 of several values.
I was very confused by the output of this code, which should have done the trick:
7:11 - mod(7:11, 5) + 5
ans =
7 8 9 10 11 12 13 14
While the actual working solution was this:
(7:11) - mod(7:11, 5) + 5
ans =
10 10 10 15 15
So this seems to be related to operator precedence! But what exactly does the first command do, and why does it output a (1,8) vector?
Addendum: I have found that the first command can also be written as:
7:(11 - mod(7:11, 5) + 5)
Which already hints towards the explanation of the observed result, but I am still curious about the whole explanation.
Here's the list of MATLAB operator precedence
As you can see, parentheses, (), are solved first, meaning that mod(7:11,5) will be done first. Then point 6), the addition and subtraction are taken care of from left to right, i.e. 11-mod(7:11,5) and then 11-mod(7:11,5)+5. Then point 7), the colon, :, gets evaluated, thus 7:11-mod(7:11,5)+5.
As you noted correctly 7:11 - mod(7:11, 5) + 5 is the same as 7:(11 - mod(7:11, 5) + 5), as seen above using operator precedence.
Now to the second part: why do you obtain 8 values, rather than 5? The problem here is "making an array with an array". Basically:
1:3
ans =
1 2 3
1:(3:5)
ans =
1 2 3
This shows what's going on. If you initialise an array with the colon, but have the end point as an array, MATLAB uses only the first value. As odd as it may sound, it's documented behaviour.
mod(7:11,5) generates an array, [2 3 4 0 1]. This array is then subtracted from 11 and 5 is added [14 13 12 16 15]. Now, as we see in the documentation, only the first element is then considered. 7:[14 13 12 16 15] gets parsed as 7:14 and will result in 8 values, as you've shown.
Doing (7:11) - mod(7:11, 5) + 5 first creates two arrays: 7:11 and mod(7:11,5). It then subtracts the two arrays elementwise and adds 5 to each of the elements. Interesting to note here would be that 7:12 - mod(7:11, 5) + 5 would work, whereas (7:12) - mod(7:11, 5) + 5 would result in an error due to incompatible array sizes.

Why 1.001298642*10^9 == 1001298642 is returning false? [duplicate]

This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Is floating point math broken?
(31 answers)
Closed 3 years ago.
While I was working on Collatz Conjecture, I seen that there was a starting point 1.001298642*10^9 where I were getting 1047 iterations to reach 1. And against these iterations my matlab programming was returning 1001298642 in a string which is 1.001298642*10^9. But when I further work and tested on Matlab
>> 1.001298642*10^9 == 1001298642
ans =
logical
0
which means 1.001298642*10^9 is not equal to 1001298642. But actually both values are same.
I also tested these values on R Studio and same result I got. What is the problem. Am I doing some mistake?
You are using the wrong operator here. ^ is Bitwise XOR. You should be using ** for Exponentiation opeartor
Note that, this will return false because 1.001298642 * 10 ** 9 returns 1001298642.0000001 and not 1001298642 (More on that here: Is floating point math broken?)
console.log(1.001298642 * 10 ** 9 == 1001298642)
console.log(1.001298642 * 10 ** 9)
But, it is interesting that the snippet returns true. 1.001298642*10^9 == 1001298642 enters the if block because the expression returns 10 which is a truthy value:
console.log(1.001298642*10^9 == 1001298642)
In the Operator precedence table, Equality operator(==) appears before Bitwise XOR (^). If you check the the expression in AST Explorer, it is grouped like this:
(1.001298642*10)^(9 == 1001298642)
10.01298642 ^ false
10.01298642 ^ 0 // coerced to zero

Fibonacci in swift playground [duplicate]

This question already has answers here:
Simple Swift Fibonacci program crashing (Project Euler 2)
(2 answers)
using for loop for fibonacci series to print values it will print up to 47 values above that it shows error [closed]
(1 answer)
Fibonacci Calculator stack overflows at 93nd number in Swift [duplicate]
(2 answers)
Closed 4 years ago.
can someone please guide me why this code that i wrote only works until userInput is less than 94 ?
func calculateFibonacciFucntionUntil(userInput: Int) {
var array = [0 ,1]
for i in 2...userInput {
array.append(i)
array[i] = array[i - 1] + array[i - 2]
print(array[i])
}
}
calculateFibonacciFucntionUntil(userInput: 10)
The issue is that Int can only store 64bit numbers (on 64bit platforms) and Fibonacci 95 is bigger than the maximum number that can be stored on 64bits.
Fibonacci 95 is 31 940 434 634 990 099 905, while the biggest number Int64 can hold is 2^63-1 = 9 223 372 036 854 775 807.
You can use Decimal for storing larger numbers than what Int can hold Decimal.greatestFiniteMagnitude is 3.4028236692093865e+165.
However, Swift doesn't have a built-in type for storing arbitrarily large numbers, so if you want to do that you'll either need to use a 3rd party library or implement an appropriate data type yourself.

How to use logical indexing on multiple criteria [duplicate]

This question already has answers here:
Function for 'does matrix contain value X?'
(4 answers)
Closed 5 years ago.
I have a table that has a column containing values between 1 and 20. I want to filter the table such that I can show only certain discrete values, i.e. 3, 10, 12, and 19. The problem is writing this is cumbersome, especially since I want to write other criteria for filtering too.:
i = tb.subn==3 | tb.subn==10 | tb.subn==12 | tb.subn==19
If I use i = tb.subn==[3 10 12 19] then I get a :x4 boolean matrix. How can I get that into just one column?
If it were &, I suppose I could use prod(tb.subn==[3 10 12 19],2) but I can't figure out or.
Thanks.
You can use ismember(tb.subn, [3, 10, 12, 19])
i = all(tb.subn==[3 10 12 19], 2);
Note that this works only for very late matlab, while you solution will work better for all versions.

Matlab, how to convert a string of integers into a vector? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
convert string to number array in matlab
Is there a simple way in Matlab to convert a string like this
'123456789'
into a vector like this ?
[1 2 3 4 5 6 7 8 9]
If all you have is contiguous characters from 0 to 9:
v = double(s)-'0';
double(s) converts a string into an array where each element is the ASCII code of the corresponding character. To obtain the numberic values we subtract '0' (which is in fact 48 in ASCII) and since digits have a sequential representation in ASCII code ('1' = 49, '2' = 50, etc.) we end up with intended result.
one way would be using regexp for this. But of course it only works for single digit numbers.
>> str = '123456789';
>> num = regexp(str,'\d')
num =
1 2 3 4 5 6 7 8 9