How to alter given code to prevent/remove 'NaN' output from corrcoef function

5 views (last 30 days)
I am currently writing a function with code featured below,
function PlaceHolder = prcc(varargin)
for i = 2:nargin
if length(varargin{i})==length(varargin{i-1})
else error('all input arrays must be the same length')
end
if nargin == 0
error('No input arguments')
end
if nargin == 1
error('Not enough input arguments. Inputs must include at least two vectors')
end
if nargin > 1
for i=1:nargin
Intermediate{:,i}=[varargin{i}];
Intermediate{:,i} = sort([Intermediate{:,i}], 'descend');
end
Parameters=Intermediate(:,1:end-1);
END = Intermediate(:,end);
Intermediate={};
PRCCMatrix = [];
for i=1:nargin-1
Individuals{1,i} = Parameters(:,i);
Absences{1,i} = Parameters(:,i);
[~,~, ResidualsAbsences{1,i}] = regress(END{1,1}, cell2mat(Absences{1,i}));
[~,~, ResidualsIndividuals{1,i}] = regress(cell2mat(Individuals{1,i}), cell2mat(Absences{1,i}));
PlaceHolder{1,i} = corrcoef(ResidualsIndividuals{1,i}, ResidualsAbsences{1,i});
end
bar(diag(PRCCMatrix), 'stacked')
end
end
I am attempting to remove all instances in which corrcoef ouputs NaN. I understand that one reason for these outputs is two data points from ResidualsIndividuals and ResidualsAbsences having the same value, though I do not believe that this is the case here. I have already verified that all inputs into corrcoef are numbers instead of NaN and I am unsure where to go from here.
For reference, the data used to reproduce this error is given below:
for i=1:300
a(i,1) = unifrnd(0,1)
b(i,1) = unifrnd(0,1)
c(i,1) = unifrnd(0,1)
d(i,1) = unifrnd(0,1)
f(i,1) = unifrnd(0,1)
end
out = 2*a+3*b+4*c+5*d+6*f
prcc(a,b,c,d,f,out)
  10 Comments
dpb
dpb on 10 Oct 2022
Same issue going on here as in the latter of the above two -- OP is creating a result "variable" that is, by definition, a linear combination of the other variables and so when computes the partial correlations controlling for all the other variables, his system is also rank deficient and the result is the correct and expected one for the case; simply cannot be otherwise.

Sign in to comment.

Answers (0)

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!