Is there a way to use the calling bar reference inside a Ref function - amibroker

When we do a Ref(var1, -5) we get the value of array var1 from 5 bars ago. For example lets say we are at bar 100 looking back using ref.
Ref(HHV(H, rangeStoredinBarNumber100), -5);
value of array rangeStoredinBarNumber100 of bar number 95 is used in above formula.
My question is is there a way to use the rangeStoredinBarNumber100 from the calling bar (bar number 100) in the above formula. Now you may say use the below
Ref(HHV(H, Ref(rangeStoredinBarNumber100, 5)), -5);
But I used the number 5 just for example. In my code the variable that stores value 5 is an array. Which means I have to write some thing like below
Ref(HHV(H, Ref(rangeStoredinBarNumber100, lookbackbar)), -1 *lookbackbar); //this fails
However Since lookbackbar is an array it has a different value 5 bars down (at bar number 95). And since the above is enclosed in Ref() (outer ref) whatever variable is referred within it refers to bar 95. So the above fails. Am I making myself clear?
Now is there a away I can use the lookbackbar variable of bar number 100 in bar number 95 without using Ref( ). Some thing like
Ref(HHV(H, Ref(rangeStoredinBarNumber100, Original(lookbackbar))), -1 *lookbackbar); //wishful thinking maybe
Or even better
Ref(HHV(H, Original(rangeStoredinBarNumber100)), -1 *lookbackbar); //wishful thinking maybe
Here Original(lookbackbar) refers to lookbackbar array value from bar 100. There is NO function called Original in AFL, it is just for the example. Is this possible?

Don't understand your question. Why don't you change -5 to -100 to have 100 look back bar

Related

How do I get my Julia code to assign the scalar value of a variable to an array without changing it as they variable mutates later on?

For my simulation, I have a field that is called particle.current_theta. When this field is a single variable, I assign it a new value that is called just "theta" on my line 177. This theta has its value changed further down within my code, on lines 202 and 206. I want what I have printed in my terminal as tree_theta and current_theta to be very similar to each other but not quite identical (This part of the code basically detects whether or not my particle is entering or exiting a region). You can see all this in the image below:
Now, I need to make the field I have called particle.current_theta a [1x1] array, and assign the entry in my [1x1] array the "theta" value, as usual. However, simply making particle.current_theta a [1x1] array radically changes its value within the terminal and causes my simulation to break. You can see how the value for particle.theta (printed in terminal as "current theta") is now drastically different in the code below:
I suspect that making particle.currenttheta an array is making it mutate whenever theta is changed in some of those lines below. How do I prevent that from happening, and get results that are identical to using just a single variable. To be precise, I want particle.current_theta to save the numerical information that theta has at line 177 of code but not be changed afterwards. Because of the large size of my code now and the function calls within function calls, it would be infeasible for me to be able to create a mwe that replicates this issue. However, all help and advice is appreciated, and I will respond to and clarify any questions that people may have.
If theta is a scalar (and it appears to be), then it's unlikely that changing its value is what is changing particle.currenttheta. What is more likely is that you're passing the currenttheta to some function, and changing the value of the passed argument inside the function. Julia arrays are passed to functions "by reference", in the sense that a copy is not made, and instead any changes made inside the function change the original array. When you had currenttheta as a scalar (which are passed "by value" instead), when you pass that to a function, changes made inside the function do not affect the original currenttheta's value.
So if you're passing currenttheta to a function somewhere and don't want it to be modified inside the function, pass copy(particle.currenttheta) in that call instead.
If you're not doing that, or that doesn't solve the problem, we need more of the code to figure out where the change might actually be happening. If not the whole of it, at least the parts that handle currenttheta, and the parts that print it. (Also, it would massively help with clarity if you would use consistent names in the output. Sometimes it's treetheta and current theta, other times it's theta and particle.currentheta, and it's not clear where these are being printed from and what the difference - if any - is.)

Need to create a return type function in Processing that returns whether one position will overlap with another

Good evening, I have been having some trouble with return type functions in Processing and I was wondering if someone could explain for example this one to me:
I have an array arrType[3], each index randomly stores an arc shape. I am supposed to draw these in random arrPosX and arrPosY, both of 50 positions/indexes. The assignment asks me to make a return type function to check if the shape overlaps with another, but I'm having a hard time with this as a return type only returns 1 value, or at least I think so? So how can I check both X and Y?
Use a function called intersects for this.
 The function gets the random x and y position,
 and returns whether this will overlap with any of the other shapes already in the array.
 You can assume that they are all circles for this, and check if they are too close based on the size
 Take the stroke of 3 into account to do the overlap check
Thank you in advance!

Easier way to call an absurd number of functions

So I'm calling a whole lot of functions what kind of have a similar pattern
(disclaimer:I did not write these)
They go like so:
Write_thing0<x>_object00<y>(somedata);
where <x> takes values from 0 to 6 and <y> takes values from 1 to 20.
Is there a sane way to do this, perhaps in a loop? The token-pasting operator was suggested but that doesn't work or I can't find a way to use it.
Edit:
Okay so I'll go into more depth:
Write_thing000_object001_A(uint8);
Write_thing000_object001_B(uint8);
Write_thing000_object001_C(uint16);
.................................
Write_thing000_object001_Z(uint8);
Write_thing000_object002_A(uint8);
.................................
Write_thing000_object002_Z(uint8);
/* all the way up to object 20, then thing changes to 001 and object count resets */
the pattern of parameters stays the same in each A-Z block.
I hope that makes more sense.
It can be done with x-macros:
#define CALL_ONE(i,j) call_func_##i##_##j(); // Put your function name here.
#define LIST_A(m,d) m(0,d) m(1,d) m(2,d) // Replace 0,1,2 with actual values.
#define LIST_B(m,d) m(X,d) m(Y,d) m(Z,d) // Replace X,Y,Z with actual values.
#define CALL_A(x,unused) LIST_A(CALL_ONE,x)
#define CALL_B() LIST_B(CALL_A,)
Now, CALL_B() expands to:
call_func_0_X(); call_func_1_X(); call_func_2_X();
call_func_0_Y(); call_func_1_Y(); call_func_2_Y();
call_func_0_Z(); call_func_1_Z(); call_func_2_Z();
It should be easy to adapt this for your purposes.
From your example you seem to suggest that the code looks like this:
Write_thing000_object001(somedata);
Write_thing000_object002(somedata);
Write_thing000_object003(somedata);
Write_thing000_object004(somedata);
Write_thing000_object005(somedata);
/// lots of lines
Write_thing006_object0020(somedata);
If this is exactly what your code looks like fine. But it raises a lot of questions to me
1) Does some data stay the same for each function call (or set of calls)
2) Is every function name used or is a subset of names
3) What do all these functions do? It seems likely give the name that they could be all be rewritten like this:
Write_thing_object(somedata,x,y);
then you just have one function and you can put the whole thing in a loop -- why can't you do that?
Also as a side note, this is often called a 'dispatch pattern', if you simple make this function and have it call the correct other function. You could do this efficiently by creating a 2 dimension array of function pointers. If this is what you need to do I could show you how to do that.

Matlab making new cell array from another cell array fileds

I have a cell array thiscells:
thiscells <1*40 cell>
eacharray of thiscells is 280*3
I have a function (TheFunction) that gets input of cell array that each array is X*2
How can I send a cell array to TheFunction that contain only columns1,3 from each array?
I tried
TheFunction(thiscells{:}(:,1:2)) % tring to send only columns 1,2
But it didn't work
Edit:
working with (but still looking for faster way to do so):
What is did so far
TheFunction(makingNewCells(thiscells,2));
when
makingNewCells:
[newCells]=makingNewCells(oldCells)
for ii=1:max(size(oldcells))
newCells=oldCells(:,[1 column]);
end
This question and its answers may be of help - thiscells{:}(:,1:2) is already failing without the call to Thefunction, if I am not mistaken.
For cells, the problem is even worse: add to this impossibility to double-subref that even if it did what you intended it to do, this passes 40 arguments (which are all 280x2 vectors) to Thefunction. I doubt there are (m)any legitimate uses for a function that takes 40 arguments of the same type, instead of having those wrapped in a cell. I would probably try doc cellfun first.
EDIT: just to be clear, I suggest changing Thefunction so that it accepts a cell of 40 matrices of size 280x2), then using
cell_of_twocolumned_arrays = cellfun(#(x)x(:,[1 2]),thiscells)

Strange behavior with Matlab array

I am having some trouble manually creating a histogram of intensity values from a grayscale image. Below is the code that I am using the create the bins for the plot that I want to create. The code works fine for every bin except for the last two. For some reason if the intensity is 254 or 255 it puts both values into the 254 bin and no values are accumulated in the 255 bin.
bins= zeros(1,256);
[x,y]=size(grayImg);
for i = 1:x
for j = 1:y
current = grayImg(i,j);
bins(current+1) = bins(current+1) + 1;
end
end
plot(bins);
I do not understand why this behavior is happening. I have printed out the count of 254 intensities and 255 intensities and they are both correct. However, when using the above code to accumulate the intensity values it does not work correctly.
Edit: Added the image I am using, the incorrect graph(the one I get with above code), and the correct one
A. The first problem with your code is the initial definition of bins. It seems that you come from C or somthing like that, but the definition should be- bins=zeros(1,256);
B. The second point is that you don't need the nested loop, you have a matlab function especially for that:
bins=hist(grayImg(:),1:256); % now, you don't need the pre-definition for 'bins'.
plot(bins);
C. Try to use functions like bar or imhist or hist(grayImg(:)), it may save you all this, and give a nice plot.

Resources