matrix function to do this?

  • Thread starter Thread starter Puppet_Sock
  • Start date Start date
P

Puppet_Sock

I have a column of data.

12.3
3.5
7.8
8.5
...etc.
9.4

It is 16 elements in length, but the number 16 may be different for
different cases.

These are the values of a diagonal 16x16 matrix.
12.3 0 0 0 etc.
0 3.5 0 0 etc.
0 0 7.8 0 etc.
etc.

What I want is a way to store the data as the column, but have it
behave like the diagonal matrix when I do things like mmult,
transpose, minverse, etc. It would be cool if there was something like
mdiag(a,b,c,d) that would create a matrix that was zero off diagonal,
and had the values a, b, c, d, down the diagonal.

Is there anything like that?
Socks
 
You can create a matrix from that data quite easily. Suppose the data
is in column A, with the 12.3 value in A2. Put this formula in C2:

=IF(COLUMN(A1)=ROW(A1),$A2,0)

Then copy this across for however many rows you have in your original
list, and then copy that block of formulae down as required.

Hope this helps.

Pete
 
Solved my own problem. The following bit of VBA does what I wanted.

Option Explicit
Option Base 1

Public Function myDiag(dDat As Variant) As Variant
Dim arraySize As Long
Dim v As Variant
Dim curRow As Long
Dim curCol As Long
arraySize = dDat.Count
ReDim v(arraySize, arraySize) As Variant
For curRow = 1 To arraySize
For curCol = 1 To arraySize
v(curRow, curCol) = 0
Next curCol
v(curRow, curRow) = dDat(curRow)
Next curRow
myDiag = v
End Function
 
Back
Top