# Thread: Accessing column data in multi-dimentional arrays

1. ## Accessing column data in multi-dimentional arrays

Hi Everyone,

I am new to the forum and was hoping someone could help me with what I hope i a relatively simple problem. I couldn't find my problem described in an existing topic, but I may have overlooked it. If so, I apologize.

Anyway - my problem is the following.

I am currently working on a simple program and am using multi-dimentional arrays since I have a lot of data to process for different variables.
An example of one of my arrays is the following:
Code:
`double Solution`
When I use this declaration, it is relatively easy for me to access the underlying 20x20 arrays, if I pass the array to a function as
Code:
`Solution`
However, now for a different part of my code, I would like to get all the entries(as a vector) in the first column for a specific orientation in the underlying 20 x 20 array.

An example:
Code:
`Solution[*all three values*] = {entry 1, entry 2, entry 3}`
But I have been unsuccessful to do so. Is there a simple way of doing this?

Thanks to anyone who can find the time to reply. 2. So you want to enter values to all three at once? Is this correct? 3. Originally Posted by Click_here So you want to enter values to all three at once? Is this correct?
Not, that is not what I want to do.

I want to access all the three values in the first dimension as a vector, for a given specific position for the remaining two dimensions.

Here is how I am working around the problem at the moment:
Code:
`double Uij = {DeltaU[i][j],   DeltaU[i][j],   DeltaU[i][j]};`
Thus, I am defining a vector manually, and the resulting vector has the following entries:

Code:
` Uij = {DeltaU[i][j], DeltaU[i][j], DeltaU[i][j]}`
What I was hoping was, that I could somehow select the three entries in (using point 10,10 as example):
Code:
`DeltaU[k]`
(Where k is the index for my for-loop, running from 0-2)
And have the output as a vector with the following values:
Code:
` Uij = {DeltaU[k=0], DeltaU[k=1], DeltaU[k=2] }`
Does it make sense ? 4. As I understand it you want this:
Code:
```double Uij;
for (i = 0; i < 3; i++)
Uij[i] = DeltaU[i];```
Correct?

You need a for-loop if you don't want to use the "initializer" version.

Or, do you want to achieve that the values in "Uij" are references to the corresponding values of "DeltaU" (i.e. if the values change in "DeltaU" the values in "Uij" change too and vice versa)? Then you need pointers:
Code:
`double *Uij[] = { &DeltaU, &DeltaU, &DeltaU };`
(or the corresponding for-loop version) and you can access the values in "Uij" by dereferencing:
Code:
`double x = *Uij;  // "x" has now the value of DeltaU`
Bye, Andreas 5. Originally Posted by Darkmentor the first column for a specific orientation in the underlying 20 x 20 array.
Simple: Just do not assume the vector is consecutive in memory.

Instead of a simple linear array, specify the data as a pointer, number of data items between consecutive elements (stride), and vector size (element count):
Code:
```double my_function(double *const vector, const long stride, const long size)
{
/* Element i (0 <= i < size), is (vector[i * stride]). */
}```
The strides you can trivially compute:
• (long)(&Solution - &Solution) for a row vector -- I do believe this is defined to be 1 in C
• (long)(&Solution - &Solution) for a column vector
• (long)(&Solution - &Solution) for a plane vector

Note that the stride is not in units of bytes or chars, but the data units, here doubles, so the data types matter. If you use the actual data array to compute the (three) strides, it will be correct every time. You could use for example
Code:
```#define  PLANES 3
#define  ROWS 20
#define  COLUMNS 20

double  Solution[PLANES][ROWS][COLUMNS];

const long  Solution_row = 1L;
const long  Solution_column = &Solution - &Solution;
const long  Solution_plane = &Solution - &Solution;

/* Third column vector (column 2): */
result = my_function(&Solution, Solution_column, ROWS);```
If you supply the pointer to the final element, and negate the stride, you can reverse the vector component order, too. It is sometimes very useful.

There are also two-dimensional and higher-dimensional equivalents, of course: just specify the size and stride for each dimension separately. In 2D, for example:
Code:
```double my_function(double *const plane, const long rowstride, const long colstride, const long rows, const long cols)
{
/* Element (r,c), (0 <= r < rows, 0 <= c < cols) is
*      plane[r * rowstride + c * colstride]
*/
}```
but I personally prefer to define structures (with the size and stride fields, and a pointer to the data origin). Let me know if you want examples. 6. Not quite sure what you mean by getting column elements as a vector. I believe you mean you want elements to be consecutive in single dimension arrays.
You can transpose the Solution into Solution_T so that the last axis can be referenced using a  index and its 3 elements are consecutive.
You could also generate vectors of addresses to the column oriented elements and then reference these by consecutively dereferencing these addresses. But that's just like making a loop like AndiPersti suggested with additional address arrays. Popular pages Recent additions 