If the following code is executed MATLAB makes a mistake. Can someone verify this?
floor([0.1:0.1:2]/0.01)
So what is the 129 doing here??
ans = 10 20 30 40 50 60 70 80 90 100 110 120 129 140 150 160 170 180 190 200
It is a floating point rounding error because of the colon-generated vector.
Like Rasman said, if you do:
floor((0.1:0.1:2 + eps) / 0.01)
There will be no rounding errors.
However, based on how the colon operator works, I suggest that you do the same calculation like this:
floor([(1:20)/10] / 0.01)
[Edit: following Rasman's comment, I will add that the latter approach works for negative values as well, while adding eps sometimes fails]
The bottom line is that it is better using the colon-operator with integer numbers to minimize rounding errors.
It is probably doing a floating point calculation resulting in an inexact value of 129.99999999999999... something instead of 130. and then you floor it to 129.
it's a rounding approximation brought on by the array construction. The solution would be to add eps:
floor([0.1:0.1:2]/0.01+ eps([0.1:0.1:2]/0.01))
Related
Is there a way to force MATLAB to use single precision as default precision?
I have a MATLAB code, whose output I need to compare to C code output, and C code is written exclusively using floats, no doubles allowed.
Short answer: You can't.
Longer answer: In most cases, you can get around this by setting your initial variables to single. Once that's done, that type will (almost always) propagate down through your code. (cf. this and this thread on MathWorks).
So, for instance, if you do something like:
>> x = single(magic(4));
>> y = double(6);
>> x * y
ans =
4×4 single matrix
96 12 18 78
30 66 60 48
54 42 36 72
24 84 90 6
MATLAB keeps the answer in the lower precision. I have occasionally encountered functions, both built-in and from the FileExchange, that recast the output to be a double, so you will want to sprinkle in the occasional assert statement to keep things honest during your initial debugging (or better yet put the assertion as the first lines of any sub-functions you write to check the critical inputs), but this should get you 99% of the way there.
You can convert any object A to single precision using A=single(A);
The Mathworks forums show that
in your case: system-specific('precision','8'); should do it. Try this in the console or add at the top of your script.
I have the following data representing values over a 12 month period:
1. 0
2. 253
3. 168
4. 323
5. 556
6. 470
7. 225
8. 445
9. 98
10. 114
11. 381
12. 187
How can I smooth this line forward?
What I need is that going through the list sequentially any value that is above the mean (268) be evenly distributed amongst the remaining months- but in such a way that it produces as smooth a line as possible. I need to go through from Jan to Dec in order. Looking forward I want to sweep any excess (peaks) into the months still to come such that the distribution is as even as possible (such that the troughs are filled first). So the issue is to, at each point, determine what the "excess" for that month is and secondly how to distribute that amongst the months still to come.
I have used
p = find(Diff>0);
n = find(Diff<=0);
POS = Diff(p,1);
NEG = Diff(n,1)
to see where shortfall/ excesses against the mean exist but unsure how to construct a code that will redistribute forward by allocating to the "troughs" of the distribution first. An analogy is that these numbers represent harvest quantities and I must give out the harvest to the population. How do I redistribute the incoming harvest over the year such that I minimise excess supply/ under supply? I obviously cannot give out anything I haven't received in a particular month unless I have stored some harvest from previous months.
e.g. I start in Jan, I see that I cannot give anything to the months Feb to Dec so the value for Jan is 0. In Feb I have 253- do I adjust 253 downwards or give it all out? If so by how much? and where do I redistribute the excess I trim between Mar to Dec? And so on and so forth.. How do I do this to give as smooth (even) a distribution as possible?
For any month the new value assigned to that month cannot exceed the current value. The sum for the 12 months must be equal before and after smoothing. As the first position January will always be 0.
Simple version, just loops through and if the next month is lower than the current month, passes value forward to equalise them.
for n = 1:11
if y(n)>y(n+1);
y(n:n+1)=(y(n)+y(n+1))/2;
end
end
It's not very clear to me what you're asking...It sounds a bit like a roundabout way of asking how to fit a straight line to data. If that is the case, see below. Otherwise: please clarify a bit more what you want. Provide a toy example input data, and expected output data.
y = [ 0 253 168 323 556 470 225 445 98 114 381 187 ].';
x = (0:numel(y)-1).';
A = [ones(size(x)) x];
plot(...
x, y, 'b.',...
x, A*(A\y), 'r')
xlabel('Month'), ylabel('Data')
legend('original data', 'fit')
I dont get exactly what you want either, maybe something simple like this?
year= [0 253 168 323 556 470 225 445 98 114 381 187];
m= mean(year);
total_before = sum(year)
linear_year = linspace(0,m*2,12);
toal_after= sum(linear_year)
this gives you a line, the sum stays the same and the line is perfectly smooth ...
I have a vector that consists of some numbers in the following way:
A = [153 244 253 353 453 530 653 ...]
The pattern is that there is always 153,253,353,...,2353 (these represent time i.e. 1:53am,...11:53pm) for a day. In between these *53 numbers there are some numbers that I don't wish to keep them. For example between 353 and 453, a 433 appears which needs to be removed from the vector. So the final result I wish to get is vector
A = [153 253 353 ...2353]
(of course in the original vector I have, this pattern for one day is repeated for a whole year).
Any thought on how to do this?
I would really appreciate any answer.
An alternative (and possibly faster) answer to Oleg is to use the modulus operator:
A((mod(A,100))==53)
Keep only the 53'' of each hour:
idx = ismember(A,53:100:2353);
A(idx)
I know a similar question has been asked before with C#
Difference between 2 numbers
But I need to know if objective-c provides some function to find the difference between 2 numbers, (2 NSIntegers, to be specific)
For example the difference between:
100 and 25 is 75
-25 and 100 is 125
100 and -25 is also 125
-100 and -115 is 15
//I know I'm using the same example as the previous
Any help is very much appreciated
I think basic math is something that every programming language does: abs(num1 - num2) will work.
Remember that abs() works for integers and will automatically round any non-integer arguments in addition to performing the absolute value function. My programs tend to be full of fractional data so I end up using fabs() a lot.
The points generated should be something like this-
21 32 34 54 76 34
23 55 67 45 75 23.322
54 23 45 76 85.1 32
the above example is when k=6.
How can I generate such a cluster of say around 1000 points and vary the value of k and the radius of the cluster.
Is there any built-in function that can do this for me? I can use any other tool if needed.
Any help would be appreciated.
Have a look at ELKI. It comes with a quite flexible data generator for clustering datasets, and there is a 640d subspace clustering example somewhere on the wiki.
Consider using d for the dimensionality, as when you are talking about clusters k usually refers to the number of clusters (think of k-means ...)
I think you would need to write your own code for this. Supposing your center is at the origin, you have to pick k numbers, in sequence, with the constraint at every step that the sum of the squares of all the numbers upto (and including) it must not exceed the radius of the hypersphere squared. That is, the k th number squared must be less than or equal to the radius squared minus the sum of the squares of all previously picked numbers.
If you have the stats toolbox this is easy
http://www.mathworks.co.uk/help/toolbox/stats/kmeans.html
Otherwise, you can quite easily write the code yourself using Lloyds algorithm.