#๐ Converting 2D into 1D
127 messages ยท Page 1 of 1 (latest)
@vivid ingot
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
wouldn't it just be x*16+y?
ig it depends on how you stored it
ew why
y not
I mean it doesn't really matter just add 1 but ew
okk
it breaks indexing conventions. it's just weird
u sure it's going to work?
no, I haven't seen how you flattened your 16x16 matrix yet
but assuming you just laid each index out sequentially it should work

from x=0, y=15 to x=15,y=0?
but the one is in the bottom left
I calculated and x= 3 and y=2 should be at index 19
but all the ones I've tested so far don't seem to work, or they work but for nothing else
can you send your actual code?
so the bottom left to top right thing is not set in stone?
?
the order you are indexing them in
yes
ow I get it
no it's not
I just thought it would be smarter to do it like this
you should do it top left to bottom right then
because it would be a more accurate repr of the list
that makes it easy to convert and lets you use x*16+y+1
there is no such thing as a "more accurate" representation of a flattened list
if you say
sooo
I just start to iterate from top to bottom then
but does it change the algo?
because it's technically just the same thing reversed
let's say I have a tile in position x=3 and y=2
and I'd like to get it's list index
(it's 19)
that would be
3*16+2+1
which is 51
somehow
right, because you have the x=0 list as indices 1-16, the x=1 list at 17-32, the x=2 list at 33-48, and x=3,y=0 is 49+2 == 51
so you mean I have to do x-1?
no
but x=1 and y=1 is the origin right?
oh.. you're doing 1 based indices for both.. right
is that set in stone? it just makes your algorithm harder to use
no I guess
but yeah in that case you would do x-1
no you can just remove the +1 part at the end
let me just show you a little code to demonstrate what I mean
but jus a second
if index 0 doesn't exist
like I can't insert stuff at index 0
in the list
I should then keep the +1 part right ?
anyways
show me
i'm desperate
>>> x = [[i+j for i in range(3*j,3*(1+j))] for j in range(3)]
>>> x
[[0, 1, 2], [4, 5, 6], [8, 9, 10]]
>>> flat_x = []
>>> for i in range(3):
... for j in range(3): flat_x.append(x[i][j])
...
>>> flat_x
[0, 1, 2, 4, 5, 6, 8, 9, 10]
>>> def calculate_flat_index(x,y): return x*3+y
...
>>> x[2][1]
9
>>> flat_x[calculate_flat_index(2,1)]
9
this is using 0 based indexing obviously
so you are doing a 3*3 grid right
in the example yeah
no it's still a 3x3
yeah
3 4 5
6 7 8
okaaaaay
how would that translate to 16 * 16?
will it just go up to 15
not using 0 based indexing
it'd be 3*16 == 48 + 2 == 50
alr I'm guessing
oow
and using one based indexing means
it's just +1
right?
hello?
did he go
noo
come back
ngl I'm just not that interested in calculating the 1-based indexing version but fine whatever
is your original matrix 1 based indexed as well?
i.e. x=1,y=1 is the first index?
(x-1)*grid_size + y
is the algorithm
@vivid ingot
sorry
thanks for your help anyways
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.