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:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Kelvin

I think the only way you can do it is to loop through the relevant dimension(s) of the array.

That should be straighforward.
 
Upvote 0
Thanks for the replies.

Unfortunately all of these methods use a syntax that is dependent on knowing the number of dimensions N in the array, as well as the specific dimension X to be selected, beforhand, but in this case both N and X are variables. So, for example, looping would require knowing N and X beforehand in order to write N levels of loops and writing the Xth level differently. :(
 
Upvote 0
Kelvin Stott,

Can we see your macro code for I have a N-dimensional array called "Data", where the number of dimensions N is variable...


If posting VBA code, please use Code Tags - like this:

[code]

'Paste your code here.

[/code]
 
Upvote 0
Hi Kelvin and all,

The number of dimensions N for the array can be determined at runtime, however I don't know of a simple way to use that number dynamically to reference specific elements.

This might not be possible with an unlimited number of dimensions; however as the link Hiker95 referenced points out, the maximum number of dimensions allowed is 32 and it is rare to have more than 3. Using a constraint on the maximum N, one approach would be to use Select Case statements up until that maximum.

Code:
Function Extract1D(vArr As Variant, X As Integer) As Variant
    Dim vArrayOut As Variant
    Dim lIdx() As Long
    Dim i As Long, j As Long
    Dim N As Integer ' number of dimensions

    On Error Resume Next
    '--get LBound of each dimension and number of dimensions
    Do
        N = N + 1
        ReDim Preserve lIdx(1 To N)
        lIdx(N) = LBound(vArr, N)
    Loop Until Err.Number <> 0
    On Error GoTo 0
    N = N - 1

    
    '--test for 0 Dim Array or X outside the range of Dimensions
    If X > N Or X < 1 Then Exit Function

    
    ReDim Preserve lIdx(1 To N)
    ReDim vArrayOut(1 To UBound(vArr, X) - LBound(vArr, X) + 1)
    '--build 1D array using Lbound of each dimension and each Index of dimension X.
    For i = LBound(vArr, X) To UBound(vArr, X)
        lIdx(X) = i
        j = j + 1
        Select Case N
            Case 1
                vArrayOut(j) = vArr(lIdx(1))
            Case 2
                vArrayOut(j) = vArr(lIdx(1), lIdx(2))
            Case 3
                vArrayOut(j) = vArr(lIdx(1), lIdx(2), lIdx(3))
            Case 4
                vArrayOut(j) = vArr(lIdx(1), lIdx(2), lIdx(3), lIdx(4))
            Case 5
                vArrayOut(j) = vArr(lIdx(1), lIdx(2), lIdx(3), lIdx(4), lIdx(5))
            Case 6
                vArrayOut(j) = vArr(lIdx(1), lIdx(2), lIdx(3), lIdx(4), lIdx(5), lIdx(6))
            Case 7
                vArrayOut(j) = vArr(lIdx(1), lIdx(2), lIdx(3), lIdx(4), lIdx(5), lIdx(6), lIdx(7))
            Case Else
                MsgBox "Array exceeds maximum number of dimensions allowed by function."
        End Select
    Next i
    Extract1D = vArrayOut
End Function
 
Last edited:
Upvote 0
Thanks for the replies.

Unfortunately all of these methods use a syntax that is dependent on knowing the number of dimensions N in the array, as well as the specific dimension X to be selected, beforhand, but in this case both N and X are variables. So, for example, looping would require knowing N and X beforehand in order to write N levels of loops and writing the Xth level differently. :(
Are you able to provide some further information?

Reading what you've written, it looks like you want to do something analogous to say finding the length of one edge of an irregular n-polytope without being prepared to say in advance how n is arrived at and without saying which edge.

At some stage you must make the decision of how large is n, and which specific edge are you considering.

So, at what stage of your process/problem are these decisions made, and how are they arrived at? If in Excel, by specifying them in an input box or similar, output of some VBA code, result of a formula, other ... ??? :confused: :confused:
 
Upvote 0
To use a variable number of dimensions without having specific syntax for each (as in Jerry's excellent example), I think you would have to get array location in memory using VarPtr(), get the number of dimensions and the size of each, and then calculate the offset to the element of interest based on the data type and the limits of each dimension. Then use CopyMemory to retrieve the value.

You may get some inspiration from Get Dynamic Array Information.

Like others, I can't quite imagine how you can't know the number of dimensions in advance. I also don't know how your code could create an array with a variable number of dimensions without dimension-specific syntax.
 
Upvote 0
I'm trying to write a stand-alone function, where the input variable is an N-dimensional array:

Function GET1DARRAY(NArray, X)

I also have another function NDIM which can count the number of dimensions N in NArray, but from there I get stuck...
 
Upvote 0

Forum statistics

Threads
1,215,343
Messages
6,124,405
Members
449,157
Latest member
mytux

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