Given an integer $i\in\{1,\ldots,NM\}$, find its place in a matrix of size $N\times M$?

23 Views Asked by At

The integers $N$ and $M$ are positive. Given the matrix $\mathbf{A}=\left[\mathrm{a}_{nm}\right]$ defined as follows: $\mathrm{a}_{nm}=m+(n-1)M$ for all $m\in\{1,\ldots,M\}$ and $n\in\{1,\ldots,N\}$.

The problem is: Given an integer $i\in\{1,\ldots,NM\}$, find the row $n$ and the column $m$ where $i$ is located in the matrix?

My guess is:

$n=\left\lceil{\dfrac{i}{M}}\right\rceil$ and $m=i-\left(\left\lceil{\dfrac{i}{M}}\right\rceil-1\right)M$.

1

There are 1 best solutions below

2
On BEST ANSWER

Hint Dividing both sides of $$\phantom{(\ast)} \qquad a_{nm} = m + (n - 1) M \qquad (\ast)$$ by $M$ gives that $$\frac{a_{nm}}{M} = \frac{m}{M} + (n - 1) ,$$ and since $0 < m \leq M$, we have $$n - 1 < \frac{a_{nm}}{M} \leq n ,$$ so we can recover the row number $n$ by taking the ceiling of both sides: $$\left\lceil\frac{a_{nm}}{M}\right\rceil = n .$$ We can recover the column number $m$ of $a_{nm}$ by substituting this expression into $(\ast)$ and solving for $m$.