Modified Correlation Matrix output

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
349
Office Version
  1. 2003 or older
Platform
  1. Windows
I have a UDF for correlation matrix (enclosed); can it be modified to replace the non-result 0's (zeros) with "n/a"? For example,

When I use the function I get a 1-sided correlation matrix for --

- data:
x1
x2
x3
x4
1.0000
1.0000
2.0000
4.0000
4.0000
3.0000
4.0000
2.0000
4.0000
2.0000
4.0000
2.0000
4.0000
5.0000
5.0000
2.0000

<tbody>
</tbody>

- output (the correlation matrix -- '=CorrelationMatrix(Range)):
0.0000
0.6831
0.9272
-1.0000
0.0000
0.0000
0.8919
-0.6831
0.0000
0.0000
0.0000
-0.9272
0.0000
0.0000
0.0000
0.0000

<tbody>
</tbody>

What i need is n/a in place of 0's.

Code:
Function CorrelationMatrix(rng As Range) As Variant
Dim i As Integer
Dim j As Integer
Dim NumColumns As Integer
NumColumns = rng.Columns.Count - 1
Dim Matrix() As Double
ReDim Matrix(NumColumns, NumColumns)
For i = 0 To NumColumns
For j = i To NumColumns
If j = i Then Matrix(i, j) = False Else Matrix(i, j) = WorksheetFunction.Correl(rng.Columns(i + 1), rng.Columns(j + 1))
Next j
Next i
CorrelationMatrix = Matrix
End Function
 
Last edited:

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Rich (BB code):
Function CorrelationMatrix(rng As Range) As Variant
Dim i As Integer
Dim j As Integer
Dim NumColumns As Integer
NumColumns = rng.Columns.Count - 1
Dim Matrix() As Variant
ReDim Matrix(NumColumns, NumColumns)
For i = 0 To NumColumns
For j = i To NumColumns
If j = i Then Matrix(i, j) = False Else Matrix(i, j) = WorksheetFunction.Correl(rng.Columns(i + 1), rng.Columns(j + 1))
Next j
Next i

For i = 0 To NumColumns
   For j = 0 To NumColumns
       If Matrix(i, j) = 0 Then Matrix(i, j) = "n/a"
   Next j
Next i
    
CorrelationMatrix = Matrix


End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,665
Members
449,091
Latest member
peppernaut

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