# Matrix in advanced perl programming oreilly

Hi,
On page 28 of the book 'other matrix representations', number of element (8,4) in matrix of 10 rows and 5 colums is calculated as element 38, (7 * 5 + 3).

The formula i use to calculate this number =
E = (y-1)r + x
Where E = the element result i search,
r = rows of the matrix
searching element (x,y).

in the former example we get:
E = (4-1)*10 + 8.
E = 38.

Can anyone explain how the autor got to 7*5+3 ???

50 pts. due to no real death importance, just questioning myself.
LVL 4
###### Who is Participating?

Commented:
The problem here is with the x&y. If you have a matrix that is 10 by 5, then your x is in range 0 to 9 and the y is in range 0 to 4.

If x&y are already in range, you don't need -1. The formula becomes x*5+y

0,0 = 0*5+0 = 0;
0,1 = 0*5+1 = 1;
0,2 = 0*5+2 = 2;
0,3 = 0*5+3 = 3;
0,4 = 0*5+4 = 4;
1,0 = 1*5+0 = 5;
1,1 = 1*5+1 = 6;
1,2 = 1*5+2 = 7;
..
7,3 = 7*5+3 = 38;
..
8,4 = 8*5+4 = 44;
..
9,4 = 9*5+4 = 49;

See?

Dave
0

Commented:
E = (x-1)c + (y-1)

The elements are stored in the order (1,1) (1,2) ... (1,5) (2,1) and not (1,1) (2,1) as you have calculated. Your calculation also requires a (x-1) because element (1,1) is actually element 0
0

Commented:
MARECS is correct. The formula in the example is for arrays with base ZERO (like perl) and stored in ROWS.

Other languages store arrays starting with 1, or store
in COLUMNS, or both, and the formula changes for each
of these cases.

Dave
0

Commented:
MARECS is correct. The formula in the example is for arrays with base ZERO (like perl) and stored in ROWS.

Other languages store arrays starting with 1, or store
in COLUMNS, or both, and the formula changes for each
of these cases.

Dave
0

Commented:
Dang. Double Click.. :)
0

Author Commented:
E = (x-1)c + (y-1)
E = (8-1)5 + (4-1)
E = 38.

But then element (4,2) -> 14
E = (4-1)5 + (2-1)
E = 16
This one is not correct.
can you explain? the formula you gave came to my mind too.
0

Author Commented:
as i draw both way matrixes, i sill came on 14, what wrong then?
0

Commented:
The same works for column stored arrays. The formulas are:

a) For row first, zero based: x * Rsize + y
b) For row first, one based:  (x-1)* Rsize + y - 1
c) For column first, zero based: y * Csize + x
d) For column first, one based: (y-1) * Csize + x - 1

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.