Well, I'm very new to lua, LITERALLY today began to study this. So this is my code:
local l = {1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1}
local n = table.getn(l)
local path = {{l[1], 1}}
local index = 1
for i=2,n do
if l[i] ~= l[i-1] then
index = index + 1
path[index][1] = l[i]
path[index][2] = 0
end
path[index][2] = path[index][2] + 1
end
What I want to do is to get path array (table) where zeros and ones should be grouped with their consequent amount. The output should be:
{{1, 1}, {0, 3}, {1, 3}, {0, 8}, {1, 1}}
But the problem is I get index expected, got nil error in line: path[index][1] = l[i] What is wrong with this code? index should be incremented and new item in path array should be created... But it isn't...
Index is set to to and you are attempting to index into path at position 2, which returns nil. Then you are attempting to set index 1 on nil. You need to create a table at index 2 of path. Try doing this
path[index] = {l[i], 0}
Related
I was trying to get the nullity and kernel of a matrix over the complex field in Maxima.
I get strange results, though.
I can define a matrix A:
M : matrix([0, 1, 1, 0], [-1, 0, 0, 1], [0, 0, 0, 1], [0, 0, -1, 0]);
A : M + %i * ident(4);
... for reference, it looks like this:
%i 1 1 0
-1 %i 0 1
0 0 %i 1
0 0 -1 %i
If I then compute the nullity with nullity(A), I get 3.
If I compute the rank with rank(A), I also get 3.
And if I compute the nullspace with nullspace(A), I get:
span([-1, %i, 0, 0], [-%i, -1, 0, 0], [2%i, 2, 0, 0])
But this is pretty weird, because -%i * second(...) is [-1, %i, 0, 0], which is the first vector.
And indeed, when I do NullSpace[{{i, 1, 1, 0}, {-1, i, 0, 1}, {0, 0, i, 1}, {0, 0, -1, i}}] in Mathematica, I get that the nullspace has basis [%i, 1, 0, 0] and is 1-dimensional (not 3-dimensional).
What am I doing wrong?
You are doing everything right, as far as I can tell. The problem is a bug in Maxima, which I have reported: https://sourceforge.net/p/maxima/bugs/3158/
I don't see any simple way to work around it. I am working on fixing the bug.
If I have coordinates of element in array (|row_index, column_index|) how can I change the value of element?
This is my array: element[1,1] == 1, and I need to change the value of element near it [1,2] to "1"
#data = [
[0,0,0,0,0],
[0,1,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]
]
That's how I'm finding element == 1
#data.each_with_index do |row, row_index|
row.each_with_index do |value, column_index|
if value == 1
#data[row_index+1][column_index] = 1
end
end
end
Right now your code changes the value under the cell, that contains 1, but you've stated that you need to change it on the right. I believe your code should be the following:
#data.each_with_index do |row, row_index|
row.each_with_index do |value, column_index|
if value == 1
#data[row_index][column_index + 1] = 1
end
end
end
You should note, however, since you're changing your array while iterating over it, you'll continue to write 1 to the right, until the elements are done (that will cause an error you get):
#data = [
[0,0,0,0,0],
[0,1,1,1,1], # <-- each iteration will write to the next cell
[0,0,0,0,0],
[0,0,0,0,0]
]
If it's a desired behavior, just check your indexes stay in the array borders:
#data.each_with_index do |row, row_index|
row.each_with_index do |value, column_index|
if value == 1 && column_index < row.size - 1
#data[row_index][column_index + 1] = 1
end
end
end
Otherwise, just break from 2 loops at once:
all_done = false
#data.each_with_index do |row, row_index|
row.each_with_index do |value, column_index|
if value == 1 && column_index < row.size - 1
#data[row_index][column_index + 1] = 1
all_done = true
break
end
end
break if all_done
end
You just need to assign the value directly by coordinates.
irb(main):114:0> a[0][0] = 100
irb(main):115:0> a
=> [[100, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
irb(main):117:0> a[1][2] = 1
irb(main):118:0> a
=> [[100, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]
Be careful: element[1,1] is different with element[1][1]
element[start,length]: returns a subarray starting at the start index and continuing for length elements
element[row][column]: a element of two-dimensional array
One way of doing this will be to do something like below:
#data = [
[0,0,0,0,0],
[0,1,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]
]
row = #data.find_index{ |x| x.include? 1 }
col = #data[row].find_index { |x| x == 1 }
#data[row][col.next] = 1
Disclaimer, I'm a beginner.
I have an array that is 16 digits, limited to 0's and 1's. I'm trying to create a new array that contains only the index values for the 1's in the original array.
I currently have:
one_pos = []
image_flat.each do |x|
if x == 1
p = image_flat.index(x)
one_pos << p
image_flat.at(p).replace(0)
end
end
The image_flat array is [0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
With the code above, one_pos returns [3, 3] rather than the [3, 5] that I'd expect.
Where am I going wrong?
Where am I going wrong?
When you call
image_flat.index(x)
It only returns first entry of x in image_flat array.
I guess there are some better solutions like this one:
image_flat.each_with_index do |v, i|
one_pos << i if v == 1
end
Try using each_with_index (http://apidock.com/ruby/Enumerable/each_with_index) on your array.
image_flat = [0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
one_pos = []
image_flat.each_with_index do |value, index|
if value == 1
one_pos << index
end
end
I think this is the most elegant solution here:
image_flat.each_index.select{|i| image_flat[i] == 1}
Here is a solution if you are looking for a solution that doesn't reach out of the enumerable block although it does require a chained solution.
image_flat.each_with_index.select { |im,i| im==1 }.map { |arr| arr[1] }
Its chained and will require an additional lookup so Gena Shumilkin's answer will probably be more optimal for larger arrays.
This was what I originally thought Gena Shumilkin was trying to reach until I realized that solution used each_index instead of each_with_index.
I'm writing a game using Lua and Love2d but I've hit a snag when dealing with nested tables.
I have a function that runs through a table containing numbers corresponding to walls, buttons, etc. and prints colored blocks based on the keys. An example of one of these tables would look like this:
map = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1 }
{ 1, 0, 0, 0, 0, 0, 0, 0, 1 }
{ 1, 0, 1, 1, 2, 0, 0, 0, 1 }
{ 1, 0, 0, 0, 0, 0, 0, 0, 1 }
{ 1, 1, 1, 1, 1, 1, 1, 1, 1 }
}
This works fine when rendered. However when I attempt to create this same table using a function that reads this data from a text file looking like this:
111111111
100000001
101120001
100000001
111111111
It creates a table that seems identical but it simply doesn't work when I try to render it.
So I tried debugging using a bit of code that prints out table contents and though the contents are the same, the bit of hex describing the nested tables are different. Example:
Reading the first nested table of the map file:
1 table: 0x106c5a720
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
Reading the first nested table of the manually created table:
1 table: 0x106c64120
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
What's going on here? The values are all identical but something strange is happening.
edit: Here's the bit of code that renders the map for reference:
for y=1, #map do
for x=1, #map[y] do
if map[y][x] == 1 then
print("found a wall")
love.graphics.rectangle("fill", x * 30, y * 30, 30, 30)
elseif map[y][x] == 2 then
print("found a button")
love.graphics.setColor(255, 0, 0)
love.graphics.rectangle("fill", x * 30, y * 30, 30, 30)
love.graphics.setColor(0, 0, 255)
end
end
end
When reading data from a text file, you are getting strings.
In your original map table you have numbers.
Numbers are not equal to strings.
assert(1 ~= '1')
How can I read a file like this:
11111
10001
10001
10001
11111
To a bidimensional array like this:
{{1, 1, 1, 1, 1},
{1, 0, 0, 0, 1},
{1, 0, 0, 0, 1},
{1, 0, 0, 0, 1},
{1, 1, 1, 1, 1}}
In Lua?
This is what I thought of:
for i = 1, number_of_lines do
current_line = map_file:read("*line")
character_array = {}
for i = 1, #current_line do
table.insert(character_array, current_line[i])
end
end
However, I don't know how to get 'number_of_lines', this is, the number of lines in a text file with Lua. How can I do it?
Also, if there's some other easier way please tell me about it.
You don't need to get the number of lines. Just keep going until you run out of lines.
local line_data = {}
for line in map_file:lines() do
local character_array = {}
for i = 1, #line do
character_array[#character_array + 1] = line[i];
end
line_data[#line_data + 1] = character_array
end