Solved

Matrix in advanced perl programming oreilly

Posted on 2001-06-20
8
315 Views
Last Modified: 2012-06-27
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 ???

Thanks in advance(d perl programming)

50 pts. due to no real death importance, just questioning myself.
0
Comment
Question by:jbrugman
  • 5
  • 2
8 Comments
 
LVL 5

Expert Comment

by:marecs
ID: 6210185
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
 
LVL 5

Expert Comment

by:dcgames
ID: 6210724
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
 
LVL 5

Expert Comment

by:dcgames
ID: 6210725
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
 
LVL 5

Expert Comment

by:dcgames
ID: 6210728
Dang. Double Click.. :)
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Author Comment

by:jbrugman
ID: 6212981
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
 
LVL 4

Author Comment

by:jbrugman
ID: 6212993
as i draw both way matrixes, i sill came on 14, what wrong then?
0
 
LVL 5

Accepted Solution

by:
dcgames earned 50 total points
ID: 6215971
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
 
LVL 5

Expert Comment

by:dcgames
ID: 6215982
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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now