Feb 1, 2009, 05:25 PM  #1 
Adding matrices, help anyone?
I have an assignment that states:
2. When you are adding two matrices together, you add similar elements together and place them in a new matrix. Because of this, you need to have matrices with identical sizes. Create a Matrix class, with methods such as add(Matrix a), subtract(Matrix a) and multiplyScalar(double n). I have no idea where to start, would anyone be able to help:? 

0 
Feb 1, 2009, 06:04 PM  #2 
By assignment, do you mean for school? We need to know so we don't do homework for you.
What language are you using? Have you created a class before? Do you know what a matrix is in mathematical terms? Is this of integers? Real numbers? Do you know what primitive to use for each element? Are the functions supposed to return a new matrix, or modify the matrix they are called on? Let us know and we'll see what we can do? Lee 

0 
Feb 1, 2009, 06:14 PM  #3 
Yes it is for school.
It's in Java. I could use a refreshing on what a matrix is and yes I've created a class before. I don't know if it's supposed to return new or modify the matrix. What I posted is all I got. 

0 
Feb 1, 2009, 06:31 PM  #4  
Quote:
So was there an earlier assignment to create a matrix class? It seems odd that he/she/they would have you build add and subtract methods without first covering how to model a matrix in a class. If I were doing the assignment, I would also assume each element is as integer for now. Once you have that working, you can always go back and generalize it. I always like to start simple with some basic assumptions and then build from there. Here is what you need to know about matrix addition and subtraction summarized in one page: http://www.purplemath.com/modules/mtrxadd.htm 

0 
Feb 1, 2009, 06:39 PM  #5 
Ok ok I know what a matrix is now.
We might have done them before? but I really don't remember. 

0 
Feb 1, 2009, 07:01 PM  #6 
So do you guys know how I can get started off?
Or a resource that would help? 

0 
Feb 1, 2009, 07:04 PM  #7  
Quote:
If not, then you need to start with a basic matrix class (java class, I mean) before you even bother with addition and multiplication. Once you work out a basic class that can store a matrix, post it up here and I'm sure then people can help you make it better. (or help you make it work if for some reason it doesn't work for you.) Just think about ways you can store integers where you have m rows of them and n columns. 

0 
Feb 1, 2009, 07:14 PM  #8  
Quote:
A matrix is basically a 2dimensional array of numbers characterised by its numbers of rows and columns. You can also think of a matrix as a list of either row vectors or column vectors. For example, the matrix 0 1 2 3 4 5 6 7 8 9 0 1 can be thought of as the list of row vectors [ [0 1 2 3], [4 5 6 7], [8 9 0 1] ] or the list of column vectors [ [0 4 8], [1 5 9], [2 6 0], [3 7 1] ] When thinking through operations between matrices you might find the row/column interpretation easier to follow. For example, to multiply two matricies C = A*B, you multiple the row vectors of A by the column vectors of B. This puts the condition that the number of columns in A must equal the number of rows in B, and the resulting matrix C will have the property of the number of rows equal to the number of rows in A, and the number of columns will equal the number of columns of B. When you come to implement your Matrix class though, it will be far easier to store the matrix as a 2dimensional array of floats, rather than an array of row or column vectors. Also you'll need to save the dimensions of the matrix in your Matrix class. That way you will be able to check that the number of columns and rows in the operands make sense for the particular operation you are implementing, eg as in multiplication above. My vote would be to have multiply, add etc return new matrices. This makes it far easier to write expressions which involve the same matrix more than once, eg if you wanted to calculate D = C *( C + B ). b e n 

0 
Feb 1, 2009, 07:40 PM  #9 
People have been pretty clear on what you have in front of you. Just to make something clear, the way i read it, you just have to multiply every element of the matrix by the scaler, keeping them in the same positions. I just looked again, and it looks like multiplyScaler takes a double, so i guess the assumption is that the matrix should be of doubles.
So essentially, i would represent the matrix as: Code:
double myMatrix[][]; Your operations just become nested loops, either adding/subtracting the elements at the same positions, or multiplying each element by the scaler in the case of your multiply. I guess returning a new Object makes sense... but you should verify this, because i could imagine both ways working. Since there's no operator overloading, there's not a lot of expressions you can build with objects that will "look" like standard mathematical expressions, so returning a new object isn't that big of a boon. Lee 

0 
Feb 1, 2009, 08:23 PM  #10  
Quote:
Code:
c = Matrix.multiply( a, b ) ; a.multiply( b ) ; b e n 

0 
Feb 2, 2009, 03:02 AM  #11 
... and yet, people keep complaining that the availability of operator overloading in C++ is bad and dangerous
__________________
Computer Programming: An Introduction for the Scientifically Inclined So how much does an iPhone developer make? My iPhone games: Sjoelen, Mazer (free) 

0 
Feb 2, 2009, 08:52 AM  #12 
Ok i'm at school right now but when I get home I'll post what I got so far.
Thanks for the help. 

0 
Feb 2, 2009, 12:37 PM  #13  
Quote:
I guess I like the idea of operators for primitives and explicit method calls for objects, but that's just personal preference. Lee 

0 
Feb 2, 2009, 08:39 PM  #14 
import java.util.Scanner;
public class tenpointtwo { public static void main(String[] args) { String input; Scanner in = new Scanner(System.in); final int ROWS = 3; final int COLUMNS = 3; String[][] myMatx = new String[ROWS][COLUMNS]; for (int r = 0; r < ROWS; r++) for (int c = 0; c < COLUMNS; c++) { input = in.next(); myMatx[r][c] = input; } } } Ok, I've got the class done. Seems like I've got to add another matrix to it now. I really have no clue how to do that; I think the teacher taught us how the first day of class but I was sick. I could ask him tomorrow but I really wanna see if I can get this thing done tonight since I've still got 2 other programs for this chaper to do! I'm going to check the book and other sites but if someone could respond that would be super sweet. 

0 
Feb 2, 2009, 09:06 PM  #15  
Small tip: use code tags when you post so we can read the code.
Code:
import java.util.Scanner; public class tenpointtwo { public static void main(String[] args) { String input; Scanner in = new Scanner(System.in); final int ROWS = 3; final int COLUMNS = 3; String[][] myMatx = new String[ROWS][COLUMNS]; for (int r = 0; r < ROWS; r++) for (int c = 0; c < COLUMNS; c++) { input = in.next(); myMatx[r][c] = input; } } } Quote:
You've made a class with a main() that you can use to run your program. But the next step is to make a Matrix class that stores the matrix. Right now you're just storing your matrix in a 2D array. (Your Matrix class might be little more than a wrapper around a 2D array if you choose to do it that way.) 

0 
Feb 12, 2009, 06:11 PM  #16 
Ok sorry it took so long to respond again I had some stuff going on.
So how would I store it in a matrix class? 

0 
«
Previous Thread

Next Thread
»
Thread Tools  Search this Thread 
Display Modes  
Similar Threads  
thread  Thread Starter  Forum  Replies  Last Post 
Adding a Custom Phone Label when adding or Editing contacts  Chris Kavanagh  iOS 7  0  Oct 20, 2013 04:28 PM 
Adding Printer  Itsallkosher  OS X 10.8 Mountain Lion  5  Feb 5, 2013 01:08 PM 
Resolved: Adding a Printer  chattahoochee  MacBook  10  Jan 10, 2013 08:42 AM 
Adding a line  Maverick713  iPhone  1  Sep 3, 2012 06:42 PM 
Adding FB Account in ML  TOMIMOT  OS X 10.8 Mountain Lion  3  Jul 25, 2012 11:48 PM 

All times are GMT 5. The time now is 02:23 PM.