I have a 2x2 matrix A stored in data_

1 3 2 4 (column major order)

and a 2x2 matrix B stored in b.data_

5 7 6 8

and I'm using the function below to append (stick underneath) B to A.

so my result should be 1 3 5 7 2 4 6 8

however the result I'm getting is 1 3 0 0 2 4 6 8

so copy[2] and copy [3] are not getting calculated correctly yet I've worked it out on paper and it seems fine? Can anyone please spot what is wrong?

Thanks in advance!

void Matrix::AppendBelow(Matrix b)
{
    vector<double> copy;
    copy.resize (data_.size()+b.data_.size());
    for (int i = 0; i < data_.size(); i++)
    {
        copy[i] = 0;
        copy[i*mdim_ - (i % mdim_)] = data_[i];
        copy[i*mdim_ + (((i+1) % mdim_) + 1)] = b.data_[i];
    }

    data_.resize (copy.size());
    for (int k = 0; k < data_.size(); k++)
    {

        data_[k] = copy[k];
        cout<<" "<<data_[k]<<" ";
    }
}

Nevermind - fixed it.

what is so sexy about zebra......

just out of curiosity, was it because of this, your loop only initialised copy[0] and copy[1] but not copy[2] and copy[3]?

for (int i = 0; i < data_.size(); i++)    
{        
    copy[i] = 0;        
..
}

Hi Techi,

I'm not exactly sure why, but putting

copy = 0;

into its own separate loop fixed the problem...I still used
for (int i = 0; i < data_.size(); i++) in the separate loop, so I'm baffled! data_.size() is 8 so it would have initialised copy [2] and [3].

no, i think u confused array size A with array size C (let's call them A B C for simplicity. data_.size is the size of A.

i've simplified this:

for (int i = 0; i < data_.size(); i++)    
{        
    copy[i] = 0;        
    copy[i*mdim_ - (i % mdim_)] = data_[i];        
    copy[i*mdim_ + (((i+1) % mdim_) + 1)] = b.data_[i];    
}
===========================
for (int i = 0; i < 2; i++)    
{        
    c[i] = 0;        
    c[i*2 - (i % 2)] = a[i];        
    c[i*2 + (((i+1) % 2) + 1)] = b[i];    
}

a=1,3   2,4
b=5,7   6,8

when i=0
    c[0] = 0
    c[0] = a[0] = 1,3
    c[2] = b[0] = 5,7   


when i=1
    c=[1] = 0
    c=[1] = a[1] = 2,4
    c=[3] = b[1] = 6,8

can u see some initialisation missing?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.