Hello everyone,
I need your help and I will be very happy if you can. I have a 3D matrix like this:
A[x][y][z]={
{ {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} , {3, 2, 23, 2}},
{ {13, 4, 56, 3}, {5, 9, 3, 5}};
the difficulty for me , as you can realize the "y can change for each x , in the first line it is 4 , in the second line it is 2. So , how can I slice this 3D array for each x . So, I want to obtain ; for x=0= { {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} , {3, 2, 23, 2}},
for x=1; { {13, 4, 56, 3}, {5, 9, 3, 5}}.
type def the type of A[y][z].
take a pointer to your new type, call it foo.
foo = &A[which x];
and now you have foo as a 2d matrix such that foo[somey][somez] == A[which x][somey][somez], right?
*** -> This is a POINTER, so if you CHANGE FOO you ALSO change A!!! <---
you can make a copy if you need a copy.
Did you know that valarray has a slice that can probably do what you want built into it?
Have you considered doing this in 1-d and faking the offsets for the 3 dimensions? That is easier to reshape and chop up, assuming you know pointers reasonably well.
depending on what you are doing, it can be useful to store 3d as an array of 2d. Which, yes, is what you have, but I mean more directly, so it is already sliced for you, if that is what you really need and the 3d isn't used as such. EG make a 2d matrix and take an array of those.