# How can I make a matrix from an array

#### hollandamsterdam

##### New Member
Hi,

Can anyone tell me how to make a matrix from an array?
My problem is the following:
My data looks like:
X1 X2 X3 X4
1 2% 3% 4%
2 1% 4% 8%
3 9% 2% 3%
..
40 1% 2% 1%

So my array looks like: array(1 to 40, 1 to 3)
Where array(2,1) = 1%, array(2,2) = 4%, array(2,3) = 8%
Now how can I make a matrix of this??
So how can I create the following:
matrix(1)=(2%,3%,4%)
matrix(2)=(1%,4%,8%)
and so on

Now I want to be able to create the following matrix in VBA:
matrixnew = application.Mmult(matrix(2), application.Transpose(matrix(1)))
Thats my goal!

So if anyone could help me out please??? I would really be greatfull for ANY help, because I have been looking for hours for a sollution, but i am not getting there..

Greetings,

Pascal.

### Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

#### jim may

##### Well-known Member
set it up as follows:

For i = 1 to 40
For j = 1 to 3
myarr(i, j) = cells(i, j)
Next j
Next i

#### mikerickson

##### MrExcel MVP
Code:
``````Dim myArray As Variant, mySecondArray As Variant

myArray = Range("A1:C40").Value

mySecondArray = Application.MMult(myArray, Application.Transpose(myArray))``````

#### hollandamsterdam

##### New Member
Hi guys,

Thank you very much for your help. But I am not there yet: an array(1 to 40, 1 to 3) I already have. Now I need to make matrices from this array.

So if the data is: Range("A1:D40") I need to be able to refer to the following matrices:
Matrix(1)=Range("B1:D1")
Matrix(2)=Range("B2:D2")
...
Matrix(40)=Range("B40:D40")

Once I have this, the matrix multiplication is no problem.

Greetings,

Pascal.

#### mikerickson

##### MrExcel MVP

Code:
``````Dim Matrix(1 to 40) as Variant
Dim i As Long
For i = 0 to 39
Matrix(i+1)=Range("B1:D1").Offset(i,0).Value
Next``````

#### hollandamsterdam

##### New Member

But I am not there yet..

This is my code:
Range("A4:D40").Select
datamatrix = Selection
datamatrix (1)
datamatrix (2)
...

Is there not a short way for it that to say like:
matrix(1) = (datamatrix(1,2),datamatrix(1,3),datamatrix(1,4))
matrix(2) = (datamatrix(2,2),datamatrix(2,3),datamatrix(2,4))

Greetings,

Pascal.

#### pgc01

##### MrExcel MVP
Hi Pascal

You can use the worksheet function Index().

Example:

Code:
``````Sub Test()
Dim datamatrix, matrixnew

datamatrix = Range("B2:D41").Value

' Your post: matrixnew = application.Mmult(matrix(2), application.Transpose(matrix(1)))

With Application.WorksheetFunction
matrixnew = .MMult(.Index(datamatrix, 2, 0), .Transpose(.Index(datamatrix, 1, 0)))
End With

End Sub``````

P. S. You don't have, however, to read the values into an array if you don't want to. You can do the calculations referring directly to the values in the range.

Last edited:

Replies
1
Views
58
Replies
4
Views
113
Replies
5
Views
228
Replies
5
Views
89
Replies
2
Views
457

1,132,672
Messages
5,654,665
Members
418,147
Latest member
dorkas

### 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.

### Which adblocker are you using?

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

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