Moving the pointer by a given amount in brainfuck - brainfuck

My array looks like this at the moment, data pointer at the [n]. The code will give a number from 2-10
0 10 45 45 45 45 45 45 45 45 45 0 [n] 0
I need to move the pointer to the left by n spaces without disrupting the data between n and the target location. Any ideas?

Something like
----------[+[>-<+]>]<<<<<<<<<<
should work. But also, I'd suggest rethinking your data layout to leave more working space between the values you want to avoid "disturbing".

Related

Convert from base10 to base8

Converting from base-10 to base-8 is a little trickier,
but still straightforward. We basically have to reverse the process from above. Let's start with an example: 150 of base-10.
We first find the largest power of 8 that is smaller than our number. Here, this is 82 or 64 (83 is 512). We count how many groups of 64 we can take from 150. This is 2, so the first digit in our base-8 number is 2. We have now accounted for 128 out of 150, so we have 22 left over.
The largest power of 8 that is smaller than 22 is 81 (that is, 8). How many groups of 8 can we take from 22? Two groups again, and thus our second digit is 2.
Finally, we are left with 6, and can obviously take 6 groups of one from this, our final digit. We end up with 226 of base-8.
In fact, we can make this process a touch clearer with math. Here are the steps:
150/82 = 2 remainder 22
22/81 = 2 remainder 6
6/80 = 6
Our final answer is then all of our non-remainder digits, or 226.
my question is same way if we want 65 of base-10 to convert to base-8 what happens ?
any help is appreciated..thanks in advance

select every 3rd participate from a list and make and average in matlab

I got 3 lists with grades ranging from 0-100 represting 3 different tests.
each list has an equal number of indxes (represting participates).
For example- the 1st indexes in the lists- list1,list2 and list3, are the grades of the first particiapte in the 3 different tests.
I need to make a new group (named group1) that select evey 3rd participate, starting from the first, and than calculate the avarage of this group scores.
i'll appriciate any help!!
Hopefully instead of three 'lists' you are actually using a 3 column matrix for this? e.g.
testScores = [20 48 13;
85 90 93;
54 50 56;
76 80 45
...]
From here it is trivial to select every third participant:
testScores(1:3:end, :)
and then to find the mean:
mean(testScores(1:3:end,:),2)

Rearrange distribution function Matlab

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 ...

Matlab gives wrong answer

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))

Need to generate a cluster of points in k-dimensional space in MATLAB

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.