Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com How do i multiply two matices in javascript
 User Name Remember Me? Password

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 03-28-2012, 03:37 AM PM User | #1 Sler New to the CF scene   Join Date: Mar 2012 Posts: 5 Thanks: 0 Thanked 0 Times in 0 Posts How do i multiply two matices in javascript Hi im new in javascript and im having some problems with the two dimensional arrays problems. Well basically what i need to do in this program its to multiply two matrices of the same length and print the result as a matrix Example [2 4] * [3 8] = [14 20] [3 2] [2 1] [13 26] I hope someone could help me im really stuck in this problem
03-28-2012, 03:55 AM   PM User | #2
Mishu
Banned

Join Date: Mar 2012
Posts: 306
Thanks: 1
Thanked 28 Times in 28 Posts
Quote:
 Originally Posted by Sler [2 4] * [3 8] = [14 20]
It's been a while since I multiplied matrices in high school math. If you had to do it manually, what is the process in getting [14 20] by multiplying [2 4] * [3 8]?

When you have the manual process it should be fairly easy to write up the javascript to do the calculations.

Can you post the manual proces which will be effectively the pseudo code for the task.

 03-28-2012, 04:02 AM PM User | #3 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,949 Thanks: 75 Thanked 4,307 Times in 4,274 Posts http://www.mathsisfun.com/algebra/ma...ltiplying.html His two matrices are actually Code: | 2 4 | | 3 1 | and | 3 8 | | 2 1 | each is a 2x2 matrix. __________________ An optimist sees the glass as half full. A pessimist sees the glass as half empty. A realist drinks it no matter how much there is.
03-28-2012, 04:07 AM   PM User | #4
Mishu
Banned

Join Date: Mar 2012
Posts: 306
Thanks: 1
Thanked 28 Times in 28 Posts
ok then that makes it fairly easy. But I think this is homework so I'll wait to see if sler posts their attempt first.
Quote:
 1.5) No homework assignments - Do not post your entire homework assignment and request that other members do it for you. This is considered cheating, and your thread may even be used by your school to prove your guilt. Now, you may ask for advice or help on a specific aspect of your assignment that you're having trouble with. Use common sense as far as what's acceptable in terms of soliciting help with homework assignments.

Last edited by Mishu; 03-28-2012 at 04:14 AM..

 03-28-2012, 04:13 AM PM User | #5 Sler New to the CF scene   Join Date: Mar 2012 Posts: 5 Thanks: 0 Thanked 0 Times in 0 Posts Yea sure no problem, but i think you got confused a little bit with the multiplication. In the example im multiplying two matrices with two rows and two columns so the process will look like this: [2 4] * [3 8] = [2*3+4*2=14 2*8+4*1 = 20] [3 2] [2 1] [3*3+2*2=13 3*8+2*1 = 26] [2 4] * [3 8] = [14 20] [3 2] [2 1] [13 26]
 03-28-2012, 04:17 AM PM User | #6 Sler New to the CF scene   Join Date: Mar 2012 Posts: 5 Thanks: 0 Thanked 0 Times in 0 Posts I included double asterisk so it looks better but it this only represent as multiplying this matrix[2 4] times this one [3 8] [3 2] [2 1] [2 4] * [3 8] = [2*3+4*2=14 2*8+4*1 = 20] [3 2] * [2 1] [3*3+2*2=13 3*8+2*1 = 26] [2 4] * [3 8] = [14 20] [3 2] * [2 1] [13 26]
 03-28-2012, 04:39 AM PM User | #7 Sler New to the CF scene   Join Date: Mar 2012 Posts: 5 Thanks: 0 Thanked 0 Times in 0 Posts ok ok thanks i will give you my try and you could give some hints in what can i do to improve the program
 03-28-2012, 06:13 AM PM User | #8 Sler New to the CF scene   Join Date: Mar 2012 Posts: 5 Thanks: 0 Thanked 0 Times in 0 Posts So i received some help from my brother but he uses C so i tried my best to write the program in javascript. I think that one of the cycles in the matrices is wrong cause i keep getting as undefined the last two values in each matrix. I thought it would work cause i get the first values correct. This is my code: var array=[[2,8],[3,1]] ; var array1=[[3,4],[4,5]] ; var array2=[[],[]]; var x= array.length; var y=array1.length; var j=0,k=0,i=0 alert("Matrix 1 : "); for(i = 0; i < x; i++) { for(j = 0; j <= x; j++) { alert(" "+ array[i][j]); } alert(); } var y= array1.length; alert("Matrix 2 : "); for( i = 0; i < y; i++) { for( j = 0; j < y-1; j++) { alert(" "+array1[i][j]); } alert(); } for( i = 0; i < x; i++) { for( j = 0; j < y-1; j++) { for( k = 0; k < y; k++){ array2[i][j] += array[i][k]*array1[k][j]; } } } alert("Multiply of both matrix : "); for( i = 0; i < x; i++) { for( j = 0; j < y-1; j++) { alert(" " +array2[i][j]); } alert(); } } }
 03-28-2012, 06:39 AM PM User | #9 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,949 Thanks: 75 Thanked 4,307 Times in 4,274 Posts Please, when you post code, wrap it in [ code ] ... [ /code ] tags (without spaces in them) and indent the code. __________________ An optimist sees the glass as half full. A pessimist sees the glass as half empty. A realist drinks it no matter how much there is.
 03-28-2012, 07:33 AM PM User | #10 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,949 Thanks: 75 Thanked 4,307 Times in 4,274 Posts Here. Using document.write, so it's not pretty. But it demonstrates the process thoroughly, I hope. I haven't tested it with non-square matrices, hence the "KABLOOEY". Should work if I get height and width in the right places, which I don't think they are in this code. Code: __________________ An optimist sees the glass as half full. A pessimist sees the glass as half empty. A realist drinks it no matter how much there is.
 03-28-2012, 07:36 AM PM User | #11 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,949 Thanks: 75 Thanked 4,307 Times in 4,274 Posts Ahhh...one trivial change. In red, below. Same as the example given on the aforementioned link. Code: __________________ An optimist sees the glass as half full. A pessimist sees the glass as half empty. A realist drinks it no matter how much there is. Last edited by Old Pedant; 03-28-2012 at 07:41 AM..

 Bookmarks

 Tags arrays, javascript, matrices

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 12:11 PM.