# Revision history [back]

### How to flatten 2D matrix and then copy it to a specific column in another matrix

For example, I have a 2D Matrix with 3-channels:

const auto img = imread("path.jpg", IMREAD_COLOR);
const auto ROWS = img.rows;
const auto COLS = img.cols;


And then i split it to vector to get 2D Matrix with 1-channel:

std::vector<Mat> channels{};
split(img, channels);


I want to store the channels as a Matrix of size [ROWSCOLS, 3] so i tried to do this. I thought it is supposed to flatten each Matrix in the channels above to Matrix of size [ROWSCOLS, 1], then copy it to the a specific column of in the result, but it was wrong:

Mat result(ROWS*COLS, 3, CV_32F);
auto i = 0;
for(const auto& channel : channels) {
channel.reshape(0, ROWS*COLS).copyTo(result.col(i));
++i;
}


I used a naive way which give the correct result:

for (const auto& channel : channels) {
for (auto y = 0; y < rows; ++y)
for (auto x = 0; x < cols; ++x) {
result.at<float>(y + x * rows, i) = channel.at<uchar>(y, x);
}
++i;
}


What did i do wrong with the first solution?

 2 None berak 32993 ●7 ●81 ●312

### How to flatten 2D matrix and then copy it to a specific column in another matrix

For example, I have a 2D Matrix with 3-channels:

const auto img = imread("path.jpg", IMREAD_COLOR);
const auto ROWS = img.rows;
const auto COLS = img.cols;


And then i split it to vector to get 2D Matrix with 1-channel:

std::vector<Mat> channels{};
split(img, channels);


I want to store the channels as a Matrix of size [ROWSCOLS, 3] [ROWS*COLS, 3] so i tried to do this. I thought it is supposed to flatten each Matrix in the channels above to Matrix of size [ROWSCOLS, 1], [ROWS*COLS, 1], then copy it to the a specific column of in the result, but it was wrong:

Mat result(ROWS*COLS, 3, CV_32F);
auto i = 0;
for(const auto& channel : channels) {
channel.reshape(0, ROWS*COLS).copyTo(result.col(i));
++i;
}


I used a naive way which give the correct result:

for (const auto& channel : channels) {
for (auto y = 0; y < rows; ++y)
for (auto x = 0; x < cols; ++x) {
result.at<float>(y + x * rows, i) = channel.at<uchar>(y, x);
}
++i;
}


What did i do wrong with the first solution? solution?