#How to get Diagonal elements of a 2d Array converted to 1D (linear indexing use)?

1 messages ยท Page 1 of 1 (latest)

analog mica
#

hii ๐Ÿ‘‹ i am trying to get diagonal elements of a 2d array with 1d indexing and was able to get the top-left to bottom-right of the array using the r * colNum + c, but i am having trouble with getting the formula for the top-right to bottom-left diagonal.

i've tried r * (colNum - 1 - c) but it did not work:
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1

the formula gives me 16 3 3 16

anyone knows what the correct formula for the top-right -> bottom-left diagonal is?

thank you in advance!๐Ÿ™

foggy sparrowBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

dawn flint
#

I have no clue why anyone would ever want to have that diagonal (don't get me wrong, top-left to bottom-right is insanely useful, but top-right to bottom-left kinda isn't).
Nonetheless, all you have to do is let c run from colNum - 1 to 0 instead of from 0 to colNum - 1.

#

Otherwise if you insist on letting c run from 0 upwards you just have to "invert" it in your equation: r * colNum + ((colNum - 1) - c)

analog mica
#

thank you again!

#

!close