Matrix v Array

User avatar
Jezza
5StarLounger
Posts: 847
Joined: 24 Jan 2010, 06:35
Location: A Magic Forest in Deepest, Darkest, Kent

Matrix v Array

Post by Jezza »

I hope I am explaining this right so feel free to ask questions

I have a matrix I wish to act as parameters for an array like so:

a1.1, a1.2, a1.3
a2.1, a2.2, a2.3
a3.1, a3.2, a3.3

so the matrix could hold values like so

1,0,0
0,1,0
0,0,1

I want each "row" of the matrix to be the parameter values of a function so that it will act as an input, then move to the next row and then the next.

How do I loop through this matrix row by row? ....or am I going about it wrong?
Jerry
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it

Rick Rothstein
Microsoft MVP
Posts: 87
Joined: 10 Mar 2011, 05:38
Status: Microsoft MVP
Location: New Jersey in the US

Re: Matrix v Array

Post by Rick Rothstein »

Can you describe this "matrix" for us? What exactly is it and where are its members located at? How did you load it up with values?

User avatar
HansV
Administrator
Posts: 78488
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Matrix v Array

Post by HansV »

Does this do what you want?

Code: Select all

Dim i As Integer
For i = 1 To 3
    Call MyFunction(a(i, 1), a(i, 2), a(i, 3))
Next i
Here MyFunction expects either 3 arguments, or a ParamArray of an arbitrary number of arguments.
Best wishes,
Hans

User avatar
Jezza
5StarLounger
Posts: 847
Joined: 24 Jan 2010, 06:35
Location: A Magic Forest in Deepest, Darkest, Kent

Re: Matrix v Array

Post by Jezza »

HansV wrote:Does this do what you want?

Code: Select all

Dim i As Integer
For i = 1 To 3
    Call MyFunction(a(i, 1), a(i, 2), a(i, 3))
Next i
Here MyFunction expects either 3 arguments, or a ParamArray of an arbitrary number of arguments.
Thanks Hans
Here is where I am a bit fluffy on the way to store the matrix within MyFunctionwould it be something similar to:
a(1,1)=1
a(1,2)=0
a(1,3)=0
a(2,1)=0
a(2,2)=0
...
Jerry
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it

User avatar
HansV
Administrator
Posts: 78488
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Matrix v Array

Post by HansV »

Could you provide a more detailed description of what you have / what you want to accomplish?
Best wishes,
Hans

User avatar
sdckapr
3StarLounger
Posts: 392
Joined: 25 Jan 2010, 12:21

Re: Matrix v Array

Post by sdckapr »

Something like this perhaps (if your question is specific)?
Steve

Code: Select all

Option Explicit
Sub Play()
  Dim x As Integer
  Dim y As Integer
  Dim iRow As Integer
  Dim iCol As Integer
  Dim vArray
  iRow = 3
  iCol = 3
  ReDim vArray(1 To iRow, 1 To iCol)
  For x = 1 To iRow
    For y = 1 To iCol
      vArray(x, y) = -(x = y)
    Next
  Next
End Sub

User avatar
Jezza
5StarLounger
Posts: 847
Joined: 24 Jan 2010, 06:35
Location: A Magic Forest in Deepest, Darkest, Kent

Re: Matrix v Array

Post by Jezza »

That will work great Steve, sorry for the vaguity of my original question as to why I am doing this but I have created a game and I want this matrix to act as a series of logic switches (hence the 1's and 0's) that will be used to start another function depending on the state.

I will adapt your code to pick up values stored in cells on a hidden worksheet. :thankyou:
Jerry
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it

User avatar
sdckapr
3StarLounger
Posts: 392
Joined: 25 Jan 2010, 12:21

Re: Matrix v Array

Post by sdckapr »

You can load the contents of a range directly into an array without looping, if that is your intent:

Dim vArray()
vArray = Worksheets("Sheet1").Range("A1:C15")

Steve