Modified Correlation Matrix output

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
358
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:
[TABLE="width: 256"]
<tbody>[TR]
[TD]x1
[/TD]
[TD]x2
[/TD]
[TD]x3
[/TD]
[TD]x4
[/TD]
[/TR]
[TR]
[TD="align: right"]1.0000
[/TD]
[TD="align: right"]1.0000
[/TD]
[TD="align: right"]2.0000
[/TD]
[TD="align: right"]4.0000
[/TD]
[/TR]
[TR]
[TD="align: right"]4.0000
[/TD]
[TD="align: right"]3.0000
[/TD]
[TD="align: right"]4.0000
[/TD]
[TD="align: right"]2.0000
[/TD]
[/TR]
[TR]
[TD="align: right"]4.0000
[/TD]
[TD="align: right"]2.0000
[/TD]
[TD="align: right"]4.0000
[/TD]
[TD="align: right"]2.0000
[/TD]
[/TR]
[TR]
[TD="align: right"]4.0000
[/TD]
[TD="align: right"]5.0000
[/TD]
[TD="align: right"]5.0000
[/TD]
[TD="align: right"]2.0000
[/TD]
[/TR]
</tbody>[/TABLE]

- output (the correlation matrix -- '=CorrelationMatrix(Range)):
[TABLE="width: 256"]
<tbody>[TR]
[TD="align: right"]0.0000
[/TD]
[TD="align: right"]0.6831
[/TD]
[TD="align: right"]0.9272
[/TD]
[TD="align: right"]-1.0000
[/TD]
[/TR]
[TR]
[TD="align: right"]0.0000
[/TD]
[TD="align: right"]0.0000
[/TD]
[TD="align: right"]0.8919
[/TD]
[TD="align: right"]-0.6831
[/TD]
[/TR]
[TR]
[TD="align: right"]0.0000
[/TD]
[TD="align: right"]0.0000
[/TD]
[TD="align: right"]0.0000
[/TD]
[TD="align: right"]-0.9272
[/TD]
[/TR]
[TR]
[TD="align: right"]0.0000
[/TD]
[TD="align: right"]0.0000
[/TD]
[TD="align: right"]0.0000
[/TD]
[TD="align: right"]0.0000
[/TD]
[/TR]
</tbody>[/TABLE]

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

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
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,223,099
Messages
6,170,111
Members
452,302
Latest member
TaMere

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