Thread: how to copy matrices

  1. #1
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905

    how to copy matrices

    how can you copy one 30x30 matrix in to another?

    something like this (i know this doesn't work)

    int a1[30] [30];
    int a2[30] [30];

    a1=a2;

    but, yah, if you get what i mean

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    memcpy(a1,a2,sizeof(a1));

    note that sizeof(a1) == sizeof(a2) and this only works for plain old data (ints, floats, etc..). You must include <string.h> (or <cstring> and use std::memcpy)

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    88
    i think you would need to overload the = operator : )
    what i think you overload it so that it traverses one matrix (with two for loops) and copies the current value to that of another matrix

  4. #4
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    um write a for loop?..actually two for loops

    for(int i = 0; i < 30; i++)
    {
    for(int j = 0; j < 30; j++)
    {
    a2[i][j] = a1[i][j];
    }
    }

    something like that..
    nextus, the samurai warrior

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    88
    i said two for loops

  6. #6
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    oops when i wrote that i saw 0 replies..sorry slow typer...and all credits to lithium...
    nextus, the samurai warrior

  7. #7
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    lol, no duh, for loops are an easy way out, lol

    however, it's incredibly slow that way if you have a big matrix

    I was looking for the most EFFICIENT way of doing it

  8. #8
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    how big of a matrix? and have you looked at the matrix header file...i think they use the for loop to copy....because at my school we use the vector header file...even though i wrote my own to understand what it is doing
    nextus, the samurai warrior

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    The most efficent way of doing it is to look for a way to not make the copy in the first place, barring that memcpy is usually as well optimized as you are likely to see. To go faster you have to start looking at really tweaky assembly language tricks that are almost never worth your time, unless you are shaving cycles purely for the fun of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding matrices, help with switches
    By quiet_forever in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2007, 08:21 AM
  2. calling copy constructor from template
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2005, 01:54 PM
  3. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  4. 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
  5. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 11:15 AM