Objects in Arrays Help Lua? - arrays

So, I have an array
//loop here
nummobs = nummobs + 1
Mobs = {}
Mobs[nummobs] = Entity.Init(x(locations to spawn mob), y(locations to spawn mob),"testMob")
Then, call the draw method...
for i = 0, table.getn(Mobs) do
Mobs[i].draw()
end
Error: map.lua:54(Mobs[i].draw() line): attempt to index field '?' (a nil value)... BUT IT HAS SOMETHING IN IT! right?
Anyone ever try something like this? Can anyone fix it?
Thanks
Nate

Lua uses 1-based indexes for arrays. Thus, the range of an array is [1, n] inclusive, where n is the number of elements.
More importantly, you can use ipairs and not have to write out the loop components:
for i, mob in ipairs(Mobs) do
mob:draw()
end
Oh, and never use getn; use the # length operator instead.

Related

Problem with bounds array shape in scipy.optimize constrained methods

Followup to my previous question now I have a problem with the shape of the bounds array in the constrained bfgs method.
My code is the following:
nr = 5
lag = 1
guess =numpy.array([[[ random.uniform(-1.0,1.0) for k in range(nr)] for l in range(lag)],
[[ random.uniform( 0.0,1.0) for k in range(nr)] for l in range(lag)],
[[ random.uniform(-1.0,1.0) for k in range(nr)] for l in range(lag)]])
bounds =numpy.array([[[ [-1.0,1.0] for k in range(nr)] for l in range(lag)],
[[ [ 0.0,1.0] for k in range(nr)] for l in range(lag)],
[[ [-1.0,1.0] for k in range(nr)] for l in range(lag)]])
result = optimize.fmin_l_bfgs_b( myfunc,guess.flatten(),bounds=bounds.reshape(15,2) )
As you can see I start out with a (3,1,5) shaped list of lists, which is my preferred format with which I work inside the myfunc() because it's easy to parse with nested for loops.Then this list gets crunched into a (15,) shaped numpy array to satisfy the x0 parameter's format needs, but don't worry because the X value inside the myfunc() is then transformed back into my (3,1,5) format via X=X.reshape(origshape) where the origshape global variable will save the original format. It may seem inefficient and a useless back and forth but I couldn't find a way to do it easier.
Now this works with every fmin_ function so far except with bounds like fmin_l_bfgs_b, I couldn't figure out what shape the bounded values need to be in. The documentation says:
"(min, max) pairs for each element in x, defining the bounds on that parameter."
So I thought this means 1 pair for each element, so a (15,2) shape in my situation, but when I use the code above, it gives me the following error:
TypeError: 'float' object is not subscriptable
So I guess I got the shape wrong. Please help me to fix this.
result = optimize.fmin_l_bfgs_b( myfunc,guess.flatten(),bounds=bounds.reshape(15,2),approx_grad=True )
It seems like it doesn't work without setting the approx_grad variable to True. Otherwise I guess you need to specify the fprime function.
Either way the function seems to be depreciated and using the scipy.optimize.minimize function is better.

Yielding a modified Ruby array to a block

I'm trying to turn 2 lines of ruby code into 1. For example:
def average(numbers)
result = numbers.compact
numbers.reduce(+) / numbers.length
end
I've been looking through array methods and can't find an appropriate one to turn this function into a one-liner. I had hoped something like this would work:
def average(numbers)
numbers.compact.<tap or another method> { |arr| arr.reduce(+) / arr.length }
end
Basically, I'm modifying the array (in the example I have to call compact to rid nil values), so I don't have access to the array variable, and I don't want an iterator, because I don't want to call reduce(+) and length on individual elements of the array.
Does anyone have an idea of methods I could look into?
I believe you mean for your method to be the following (reduce(:+), not reduce(+) and use result rather than numbers in the second line).
def average(numbers)
result = numbers.compact
result.reduce(:+) / result.length
end
average [1,2,3]
#=> 2
If you wish the average to be a float, change the second line to
result.reduce(0.0, :+) / result.length
There are various ways to combine the two lines of the method, but I don't prefer any of them to the above. Here are a few. (I don't see how Object#tap could be used here.)
numbers.compact.reduce(:+) / numbers.compact.length
(result = numbers.compact).reduce(:+) / result.compact.length
numbers.map(&:to_i).reduce(:+) / numbers.compact.length
Note that, even if numbers can be mutated, one cannot write
numbers.compact!.reduce(:+) / numbers.length
because numbers.compact! returns nil if numbers contains no nil elements.
In Ruby v2.4+ you can use Array#sum:
result.sum / result.length
You could change the way you call average
def average(numbers)
numbers.reduce(:+) / numbers.length
end
average(num_array.compact)

Python: Manipulate an array

I have an array that looks just like this one:
array(['00:00;1;5950;6\r', '00:10;1;2115;6\r', '00:10;2;4130;6\r',
'00:10;3;5675;6\r', '00:20;1;1785;6\r'],
dtype='|S15')
For my evaluation I need only the value after the second semicolon at each array entry. Here in this example, I need the values:
5950, 2115, 4130, 5675, 1785.
Is it possible to manipulate the array in way to get the entries I want? And how can that be solved? I know how to remove the symbols, so in the end I get the array:
['0000159506\r', '0010121156\r', '0010241306\r', '0010356756\r', '0020117856\r']
But I don't know if this is the right way to handle these problem. Do anyone know what to do? Thank you very much!
Try this:
A = array(['00:00;1;5950;6\r', '00:10;1;2115;6\r', '00:10;2;4130;6\r',
'00:10;3;5675;6\r', '00:20;1;1785;6\r'],
dtype='|S15')
list_ = [str(x.split(";")[2]) for x in A]
The best way to get these values is to use the python split function.
You can choose the delimiter. So in this case the delimiter can be a ;.
result = line.split(';')
and then just find the third value of the resulting list.
num = result[2]

Reassigning vector values using nested for-loops in matlab?

In matlab I have two vectors, ind and ind3. ind = [1 2 3 4 5] and I want to define ind3 based on ind such that I want ind(3), ind(4) and ind(5) to be ind3(1) and ind3(2) and ind3(3). so that ind3 = [ind(3) ind(4) ind(5)] but for some reason I can't do this. I thought it would be simple to do using nested for loops but it doesn't really work.
for i=3:5
for n=1:3
ind3(n,:) = ind(i,:);
end
end
By going through the for-loops logically I know why the output is wrong.. but I don't get how else to do it? Am I being stupid and missing something really simple?!
I know its probably a simple answer but can anybody help??
Thanks.
If you want ind3 = [ind(3) ind(4) ind(5)] and you want to do it in a loop you just need a single loop. Additionally, since you're dealing with vectors you just have one indexing variable.
for n=1:3
ind3(n) = ind(n + 2);
end
Maybe I'm misunderstanding your question but is this what u want:
ind3=ind(3:5)
First of all, you said that your arrays are one dimensional (they are not matrices), and in your code by calling ind3(n,:) or ind(i,:) you treat them like 2 dimensional arrays.
As long as everything is 1 dimensional here, you need just one for loop:
for i=3:5
ind3(i)=ind(i-2);
end
Explanation: here i=3,4,5. For i=3 you assign ind3[3]=ind[1], for i=4: ind3[4]=ind[2], for i=5: ind3[5]=ind[3].
Or you can simply call ind3=ind(3:5)

Is there a more efficient way of choosing an element from 1 array and to every element of the same array

I want to, for every element of an array ZAbs, compare it for equality to every element of the array itself and put them into another distinct array. I want the distinct array's elements to have the same index as the ZAbs array.
I did this by creating 4 nested for loops:
for pAbs2 = 1:400
for qAbs2 = 1:300
zAbsCompare = ZAbs(qAbs2, pAbs2);
for pAbs3 = 1:400
for qAbs3 = 1:300
zAbsCompare2 = ZAbs(qAbs3, pAbs3);
if (zAbsCompare == zAbsCompare2)
InitialZModEqualsImag(pAbs2,qAbs2) = InitialZImag(qAbs2, pAbs2);
InitialZModEqualsReal(pAbs2,qAbs2) = InitialZReal(qAbs2, pAbs2);
end
end
end
end
end
However, this runs really slowly. I can't think of a better way to do this, but as I'm inexperienced with MATLAB there's probably something I'm overlooking here. Any help?
EDIT: Fixed an error and restated the question.
You can do the comparison (not sure that's what you want) efficently with bsxfun:
comp = bsxfun(#eq, X, shiftdim(X,-2));
The result comp(m,n,p,q) is 1 if X(m,n) == X(p,q), and 0 otherwise.

Resources