-1

Extracting and storing all 6x6 postitions from multiple resampled 6x6 matrices in an array in R

i got my answer from here . How to create a 2D vector where in like 2D array

Community
  • 1
  • 1
CLIX159
  • 67
  • 1
  • 11
  • Have you tried this at all? – SirGuy Jul 13 '16 at 21:03
  • Yes you [can](https://ideone.com/Pb9KoE), but it could be better to declare a 1D vector: `vector arr(36);` and to access its elements calculating the right index: `arr[i*6 + j]` instead of `arr[i][j]`. – Bob__ Jul 13 '16 at 21:19
  • '@RadouaneROUFID' Thanks.. Though i didn't understand 'Multidimensional Vector' .so please will you provide a better way to clear my all doubts. – CLIX159 Jul 14 '16 at 08:01

1 Answers1

1

You have two alternatives. vector of vectors or a single vector (See @Bob__'s comment).

The advantage of the vector of vectors is C++ goodness (iterator bounds checking etc). The downside is a higher cost of construction/copy. rows+1 vectors must be constructed (or copied).

The benefit of the single vector of rows*cols size is that you only require a single vector construction (or copy when needed). (Another performance benefit is cache locality of the data.)

Here's how you can preserve the [row][col] syntax.

const int rows = 6;
const int cols = 6;
vector<int> x(rows*cols);

//enable using [row][col] syntax
auto a_int = reinterpret_cast<int (*)[rows]>(x.data());
// cout <<  a_int[row][col] << '\n';
David Thomas
  • 778
  • 5
  • 10
  • [link](http://stackoverflow.com/questions/823562/multi-dimensional-vector)_answer of this question is on this link... – CLIX159 Sep 17 '16 at 10:43