0

So in MATLAB, if I make a N x N x N x N x 6 dimensional matrix, I can assign values to the matrix in groups of 6 as so:

myMatrix(1,1,1,1,:) = [69, 3, 864, 21, 95, 20];

Is there an analogous way to write into an N x N x N x N x 6 dimensional matrix in C++ in groups of 6?

Also, is there an analogous way to set 6 elements equal to another 6 elements I.e myArray(N,N,N,8,:) = myArray(N,N,N,6,:)?

(No std::vector solutions please- need to keep the matrix in array form as the existing code was built for arrays using c++/CUDA and is extremely complex)

Jordan
  • 305
  • 3
  • 13
  • You may want to clarify why you cant use `stl` solutions. – Fantastic Mr Fox Jun 26 '14 at 05:55
  • Do you want this to work with whatever data types (arrays) you're using right now, or can you replace it with a different data type, as long as the matrix elements are stored in contiguous memory? – Praetorian Jun 26 '14 at 06:22
  • Arrays highly preferred as I am a noob coder and I am familiar with them – Jordan Jun 26 '14 at 06:29
  • 1
    One way to do this without `stl` would be to use expression templates, see [Implementing Matlab's colon : operator in C++ expression templates class](http://stackoverflow.com/questions/16339207/implementing-matlabs-colon-operator-in-c-expression-templates-class). You may wish to have a look at the [BlueBird library](http://www.orangeowlsolutions.com/bluebird) which implements expression templates with CUDA. – Vitality Jun 26 '14 at 07:37
  • 1
    For those who are proposing using `stl`: `stl` is not supported in CUDA kernel code. See [Using std::vector in CUDA device code](http://stackoverflow.com/questions/10375680/using-stdvector-in-cuda-device-code) and [Does CUDA 5 support STL or THRUST inside the device code?](http://stackoverflow.com/questions/15052295/does-cuda-5-support-stl-or-thrust-inside-the-device-code). – Vitality Jun 26 '14 at 07:52
  • It's cool, Jack, the matrix Is initialized on the host side code not the kernel. – Jordan Jun 26 '14 at 14:09

2 Answers2

1

with #include <algorithm>, you may use std::copy:

int myMatrix[N][N][N][N][6];
const int src[6] = {69, 3, 864, 21, 95, 20};

std::copy(std::begin(src), std::end(src), myMatrix[0][0][0][0]);

And so the equivalent of myArray(N,N,N,8,:) = myArray(N,N,N,6,:) would be:

std::copy(std::begin(myMatrix[N-1][N-1][N-1][5]),  // Source start
          std::end(myMatrix[N-1][N-1][N-1][5]),    // Source end
          std::begin(myMatrix[N-1][N-1][N-1][7])); // Dest start

Note that indexing in C/C++ start at 0 and not at 1.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
0

You can assign just like in matlab: Say you have a vector:

std::vector<std::vector<std::vector<std::vector<std::vector<int>>>>>> myVector;

Assuming it has been initialised correctly like this:

myVector(N, std::vector<std::vector<std::vector<std::vector<int>(N, std::vector<std::vector<std::vector<int>(N, std::vector<std::vector<int>(N, std::vector<int>(6, 0)))));

Gross. Try and do it differently or use push_back so it makes sense.

But, however you do it, you can then assign the lowest level vector like this:

std::vector<int> yourTempVector(6, someNumber);
myVector[1][1][1][1] = yourTempVector;
Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • I can't use std::vectors in my program as I am actually using CUDA/C++ which forbids these constructs :( – Jordan Jun 26 '14 at 05:53
  • @Jordan `Cuda` cant use `stl` types? Do you have to use arrays? – Fantastic Mr Fox Jun 26 '14 at 05:54
  • I don't know what stl is but I would really prefer to use arrays as it would mean minimal restructuring of an already highly complex program. – Jordan Jun 26 '14 at 05:55
  • `stl` types are the standard c++ library types like `vector` and `map` and are basically what makes c++ good. You can use the `CUDA` specific types as shown here: http://docs.nvidia.com/cuda/thrust/#axzz35ip8zUwF – Fantastic Mr Fox Jun 26 '14 at 05:57
  • Thank you Ben, i'll keep that as a plan B. – Jordan Jun 26 '14 at 05:59
  • Woah wait a sec, in your code example above (last part), does myVector need to be declared as an std::vector? or is it a multidimesional array? – Jordan Jun 26 '14 at 06:00
  • In my code it is a std::vector which allows assignment like that. You can do this with arrays but it is more complex because you have to make sure the memory is managed correctly. – Fantastic Mr Fox Jun 26 '14 at 06:02
  • Note: indexing starts at `1` in matLab, so the equivalent should be `myVector[0][0][0][0] = yourTempVector;`. – Jarod42 Jun 26 '14 at 07:52