How to extract a 1D array from N-dimensional array?

Kelvin Stott

Active Member
Joined
Oct 26, 2010
Messages
338
Hello,

I have a N-dimensional array called "Data", where the number of dimensions N is variable...

How can I extract a 1-dimensional array from this, comprising all elements along the Xth dimension, which are the first element along all the other dimensions?

For example, this would be equivalent to taking the values along one edge of a cubic 3D array, by specifying one of the 3 dimensions as X.

Thanks indeed for any help.

Kelvin
 
Last edited:
Thank you for you post. I agree. The code I posted was to illustrate the principle but for a real application it may make all the sense to get the values as they "roll by" instead of filling the 1D array first. I'm glad you for posted the logic for the conversion between the indices of the 1D and ND indices, that helps to understand the correspondence between the 2.</SPAN>

Another example to show how to calculate the 1D index out of the ND indices, using again the 1D array for easy debug.</SPAN>

Code:
Sub Test()</SPAN>
Dim vArrND(7 To 9, 1 To 2, 2 To 7, 3 To 5), vArr1D</SPAN>
Dim j As Long, v As Variant</SPAN>
Dim lElement(1 To 3) As Long</SPAN>
 
vArrND(8, 2, 4, 3) = "Hi"</SPAN>
vArrND(9, 1, 3, 5) = " there"</SPAN>
vArrND(7, 2, 6, 4) = "!"</SPAN>
 
' calculate the index in the 1D array corresponding to the indices in the ND array</SPAN>
' lbounds = (7,9,2,3)</SPAN>
' number of values in the dimensions = (3,2,6,3)</SPAN>
lElement(1) = (8 - 7) + (2 - 1) * 3 + (4 - 2) * 3 * 2 + (3 - 3) * 3 * 2 * 6</SPAN>
lElement(2) = (9 - 7) + (1 - 1) * 3 + (3 - 2) * 3 * 2 + (5 - 3) * 3 * 2 * 6</SPAN>
lElement(3) = (7 - 7) + (2 - 1) * 3 + (6 - 2) * 3 * 2 + (4 - 3) * 3 * 2 * 6</SPAN>
 
' redim the 1D array</SPAN>
ReDim vArr1D(0 To 3 * 2 * 6 * 3 - 1)</SPAN>
 
' copy the elements of the n-dimensional array to the 1D array</SPAN>
j = 0</SPAN>
For Each v In vArrND</SPAN>
    vArr1D(j) = v</SPAN>
    j = j + 1</SPAN>
Next v</SPAN>
 
MsgBox vArr1D(lElement(1)) & vArr1D(lElement(2)) & vArr1D(lElement(3))</SPAN></SPAN>
 
End Sub</SPAN>



Nice.

It's simpler than the one I was considering and it suggests an approach that avoids the conversion to a 1D array.

Since it is possible to compute which elements of the 1D array are of interest, just pick them as they "roll by" in the 'for each v' loop -- no need to actually create the 1D array.

So, in your example,

If j=216 or j=14 then rslt=v & rslt

Obviously, in the more general case, the indices of interest would have to be stored in an array and the result organized so that the elements are in the correct order.

Also worth noting, and it is implicit in the code, is that VBA stores multi-dimension arrays in the reverse order of the dimensions. So, in your example of a 4D matrix, the allocation can be thought of as
There is 1 4D matrix.
Each element of the 4D matrix (indexed by 1 to 4) is a 3D matrix, resulting in 4 3D matrices
Each element of each 3D matrix (indexed by 1 to 2) is a 2D matrix, resulting in 4*2 or 8 2D matrices
Each element of each 2D matrixby (indexed by 1 to 6) is a 1D vector, resulting in 8*6 or 48 1D vectors
Each element of each 1D vector (indexed by 1 to 5) is a single element, resulting in 48*5 or 240 elements.

So, the 1D equivalent of the element 1,2,2,4 is (4-1)*(2*6*5)+(2-1)*(6*5)+(2-1)*5+1 = 180 + 30 + 5 + 1 = 216
Similarly, (4,3,1,1) becomes 0 + 0 + 2 * 5 + 4 = 14.
 
Upvote 0

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Forum statistics

Threads
1,216,116
Messages
6,128,930
Members
449,479
Latest member
nana abanyin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top