Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get row and column from index?

Tags:

matrix

I'm drawing a HUGE blank here.

Everything I've found is about getting an index from a given row and column, but how do I get a row and a column from an index?

The row is easy: (int)(index / width).

My brain is suffering massive bleed trying to compute the column.

Shame on me.

like image 599
Icebone1000 Avatar asked Aug 06 '12 02:08

Icebone1000


People also ask

How do I find the row and column of a value in R?

The dataframe column can be referenced using the $ symbol, which finds its usage as data-frame$col-name. The which() method is then used to retrieve the row number corresponding to the true condition of the specified expression in the dataframe. The column values are matched and then the row number is returned.

How do I find the row and column values in Excel?

Just click the column header. The status bar, in the lower-right corner of your Excel window, will tell you the row count. Do the same thing to count columns, but this time click the row selector at the left end of the row. If you select an entire row or column, Excel counts just the cells that contain data.


1 Answers

For a zero-based index, the two operations are, where width is the width of the structure:

row    = index / width column = index % width 

Those are for C using integers, where the division rounds down and the % modulo operator gives the remainder. If you're not using C, you may have to translate to the equivalent operations.

If you don't have a modulo operator, you can use:

row    = index / width column = index - (row * width) 
like image 168
paxdiablo Avatar answered Sep 18 '22 13:09

paxdiablo