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)

6
  • You may want to clarify why you cant use stl solutions. Commented Jun 26, 2014 at 5: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? Commented Jun 26, 2014 at 6:22
  • Arrays highly preferred as I am a noob coder and I am familiar with them Commented Jun 26, 2014 at 6: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. You may wish to have a look at the BlueBird library which implements expression templates with CUDA. Commented Jun 26, 2014 at 7: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 and Does CUDA 5 support STL or THRUST inside the device code?. Commented Jun 26, 2014 at 7:52

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Brilliant. Sorry for the second question but how can I use this method to set 6 elements equal to another 6 elements I.e myArray(N,N,N,8,:) = myArray(N,N,N,6,:)
Edited to add a solution of your additional question.
Does src[6] need to be of type const int? Can i make it just int?
It may be non const.
src may be a float array, but if myMatrix is an int matrix, you will have a conversion float to int.
|
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;

8 Comments

I can't use std::vectors in my program as I am actually using CUDA/C++ which forbids these constructs :(
@Jordan Cuda cant use stl types? Do you have to use arrays?
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.
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: docs.nvidia.com/cuda/thrust/#axzz35ip8zUwF
Thank you Ben, i'll keep that as a plan B.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.