# How to update a submatrix of a multi dimension matrix in OpenCV

I am so confused why I cannot update the sub matrix of a multi dimension matrix in OpenCV. All what i have done is according to their official doc and follow some questions but it failed to update. Here is my code:

int sz[] = {3, 3, 3};
Mat block(3, sz, CV_64F, Scalar::all(0));

Mat block1 = (Mat_<double>(3, 3) << 0, 1, 2, 3, 4, 5, 6, 7, 8);

vector<Range> ranges;
Range ic = Range(0, 3);
Range jc = Range(0, 3);
Range kc = Range(1, 2);
ranges.push_back(ic);
ranges.push_back(jc);
ranges.push_back(kc);

Mat test = block(&ranges);
cout << test.total() << endl;
block1.copyTo(test);
// test = block1.clone();

printf("test matirx\n");
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
cout << test.at<double>(i, j) << endl;
}
}

for(int k=0; k<3; k++) {
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
cout << block.at<double>(i, j, k) << endl;
}
}
}


No matter I use copyTo() or clone(), the test matrix do change. But the three dimensional matrix block didn't change at all! All its value are zero! Is there something wrong with my code? How should I update the sub matrix of a multi dimensional matrix? It could be easy for 2D matrix by using Rect, but how about three dimension?

edit retag close merge delete

## Comments

I'm going to have to write something to do this unless you already have? :)