简体   繁体   English

从二维数组中取出一列并存储在一维中

[英]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:我正在尝试使用这个 9 x 3 并仅使用第 3 列存储在它自己的一维数组中:

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:但它给了我整个矩阵中的前 9 个数字:

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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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