Thread: Scanning in matrices

  1. #1
    Registered User dannyrez420's Avatar
    Join Date
    Mar 2011
    Location
    Houston,TX
    Posts
    1

    Scanning in matrices

    I have a general idea in my head but would like some advice on ways you guys
    would scan in this data and store the matrices for further calculations (ie. adding, multiplying, and using geometric series to approximate (I-A)^-1


    here is some sample data with the first integer being "n" for an n x n matrix

    2
    0.6 0.4
    0.3 0.4
    3
    0.5 0.4 0.1
    0.3 0.4 0.9
    0.1 0.1 0.1
    8
    0.3920 0.0510 0.083 0.2926 0.2380 0.0688 0.3262 0.1190
    0.0488 0.3277 0.0298 0 0.0002 0.0071 0.0003 0
    0.0168 0.0003 0.0046 0.0077 0.0004 0.0024 0.0028 0.0010
    0.0034 0.0008 0.0103 0.1586 0.527 0.0056 0.0030 0.0090
    0.0225 0.0071 0.0205 0.0461 0.0662 0.0223 0.0094 0.0110
    0.0152 0.0090 0.0062 0.0237 0.0129 0.0101 0.0800 0.0114
    0.0067 0.0057 0.0080 0.0103 0.0172 0.0128 0.0120 0.0078
    0.0083 0.0001 0.0011 0.0024 0.0021 0.0052 0.0028 0.0035

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I would dynamically allocate a 2-d array of appropriate size. There's a lesson/tutorial here: Chapter 23: Two-Dimensional (and Multidimensional) Arrays.

    You will also need to keep track of the dimensions of the matrix, since sizeof won't do what you might think. If you are going to manage several, or an unknown amount, of matrices at once, you might want to consider building a struct that contains the number of rows and columns of the matrix, and a pointer to the allocated matrix. You can read input into the matrix with fscanf or a fgets/sscanf combo. You will have to write special routines for your add, multiply, etc functions that create a new matrix of the right dimensions to store the result.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If you are using C99 you could read the first line, and since C99 allows variable length arrays, use that value to create an array of [value][value] size. Otherwise you probably should use malloc to create the array of the correct size.


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to pass mutiple matrices to a linked list
    By satty in forum C Programming
    Replies: 4
    Last Post: 08-16-2010, 09:18 AM
  2. C simple Matrices
    By Cyberman86 in forum C Programming
    Replies: 3
    Last Post: 05-07-2009, 05:20 PM
  3. adding matrices, help with switches
    By quiet_forever in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2007, 08:21 AM
  4. Multiplying matrices
    By Star Lancer in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2003, 06:07 AM
  5. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2003, 09:20 AM