简体   繁体   中英

take one column from a 2D array and store in 1D

I am trying to take this 9 x 3 and use only the 3rd column to store in its own 1D array:

3    5    8     
6    3    9     
7    5    12     
0    5    5     
1    2    3     
8    2    10     
8    3    11     
9    3    12     
4    1    5     

This is what I have for a conversion:

    int index = 0;

     // 2D to 1D conversion
     for (int r = 0; r < N; r++) 
     {
       for (int c = 0; c < 3; c++) 
       {
        end[index++] = start[r][c];
       }
     }

But it is giving me the first 9 numbers in the whole matrix:

3    5    8     
6    3    9     
7    5    12 (but vertically)

I need the 3rd column only and I don't know what I am doing wrong.

you can try this:

int index = 0;

// 2D to 1D conversion
for (int r = 0; r < N; r++)
{
    end[index++] = start[r][2];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM