Assume that I have the following piece of code in C++. I declare a matrix of size 3x3x4 which means that I have four 2D matrices of size 3x3. I want to shift the rows and columns in each 2D matrix of the big 3D matrix. I used rotate
function but it seems to work only on the last dimension i.e. I can shift among the 2D matrices but not within the 2D matrices. Is there any way to use rotate
to shift the 1st and 2nd dimensions using standard C++ libraries?
Note: What I want to achieve is something similar to what circshift(A,K)
function does in Matlab.
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
double test[3][3][4] = { { {1, 4, 7, 4}, {2, 5, 8, 4}, {3, 6, 9, 4} },
{ {1, 4, 7, 4}, {2, 5, 8, 4}, {3, 6, 9, 4} },
{ {1, 4, 7, 4}, {2, 5, 8, 4}, {3, 6, 9, 4} } };
std::cout << "Matrix shifted" << std::endl;
for (uint16_t i = 0; i < 3; i++)
{
for (uint16_t j = 0; j < 3; j++)
{
std::rotate(test[i][j], test[i][j] + 1, test[i][j] + 4);
}
}
}
Aucun commentaire:
Enregistrer un commentaire