Home > Mobile >  Creating a 3D vector. C
Creating a 3D vector. C

Time:11-13

Help me please. I have a 3d vector. I need to make a new vector from this using existing internal indices. I hope the input and output information will be clear.

Input:

    a = {
        { {1,1,1,1}, {2,2,2,2}, {3,3,3,3}, {4,4,4,4}, {5,5,5,5}, {6,6,6,6} },
        { {10,10,10,10}, {20,20,20,20}, {30,30,30,30}, {40,40,40,40}, {50,50,50,50}, {60,60,60,60} },
        { {100,100,100,100}, {200,200,200,200}, {300,300,300,300}, {400,400,400,400}, {500,500,500,500}, {600,600,600,600} },
    };

Output:

    b = {
        {{ 1,1,1,1}, {10,10,10,10}, {100,100,100,100}},
        {{ 2,2,2,2}, {20,20,20,20}, {200,200,200,200}},
        {{ 3,3,3,3}, {30,30,30,30}, {300,300,300,300}},
        {{ 4,4,4,4}, {40,40,40,40}, {400,400,400,400}},
        {{ 5,5,5,5}, {50,50,50,50}, {500,500,500,500}},
        {{ 6,6,6,6}, {60,60,60,60}, {600,600,600,600}},
    }

I don't know how to iterate over indices in a 3D array to create a new 3D array (Output). I want to create a 3D vector from the columns (n-indices) of an existing 3D vector. I have a 3D vector ('Input'). I need to make a 3D vector out of this ('Output').

#include <iostream>
#include <vector>

using namespace std;

void show3D_vector(std::vector<std::vector<std::vector<double>>>& a);
void show2D_vector(std::vector<std::vector<double>>& a);
template<typename T> std::vector<std::vector<T>> SplitVector(const std::vector<T>& vec, size_t n);


int main()
{
    
        a = {
        { {1,1,1,1}, {2,2,2,2}, {3,3,3,3}, {4,4,4,4}, {5,5,5,5}, {6,6,6,6} },
        { {10,10,10,10}, {20,20,20,20}, {30,30,30,30}, {40,40,40,40}, {50,50,50,50}, {60,60,60,60} },
        { {100,100,100,100}, {200,200,200,200}, {300,300,300,300}, {400,400,400,400}, {500,500,500,500}, {600,600,600,600} },
    }; 


}


void show3D_vector(std::vector<std::vector<std::vector<double>>>& a)
{
    for (double i = 0; i < a.size();   i)
    {
        for (double j = 0; j < a[i].size();   j)
        {
            for (double k = 0; k < a[i][j].size();   k)
                std::cout << a[i][j][k] << "  ";
            std::cout << endl;
        }
        std::cout << endl;
    }
}

void show2D_vector(std::vector<std::vector<double>>& a)
{
    for (int i = 0; i < a.size(); i  ) {
        for (auto it = a[i].begin(); it != a[i].end(); it  )
        {
            std::cout << *it << " ";
        }

        std::cout << endl << endl;
    }
}

template<typename T>
std::vector<std::vector<T>> SplitVector(const std::vector<T>& vec, size_t n)
{
    std::vector<std::vector<T>> outVec;

    size_t length = vec.size() / n;
    size_t remain = vec.size() % n;

    size_t begin = 0;
    size_t end = 0;

    for (size_t i = 0; i < std::min(n, vec.size());   i)
    {
        end  = (remain > 0) ? (length   !!(remain--)) : length;
        outVec.push_back(std::vector<T>(vec.begin()   begin, vec.begin()   end));
        begin = end;
    }

    return outVec;
}

Thank you.

CodePudding user response:

You can solve this matrix transpose more succinctly.

    for(const auto& a1 : a){
        b.resize(a1.size());
        auto b1 = b.begin();
        for(const auto& a2 : a1){
            b1->push_back(a2);
            b1  ;
        }
    }

output is

{{1,1,1,1,},{10,10,10,10,},{100,100,100,100,},},
{{2,2,2,2,},{20,20,20,20,},{200,200,200,200,},},
{{3,3,3,3,},{30,30,30,30,},{300,300,300,300,},},
{{4,4,4,4,},{40,40,40,40,},{400,400,400,400,},},
{{5,5,5,5,},{50,50,50,50,},{500,500,500,500,},},
{{6,6,6,6,},{60,60,60,60,},{600,600,600,600,},},
  • Related