Correlation coefficient calculation error

As I try to find correlation coefficient for the respective variables i.e. 'td' & 'cumulat', in for loop, some error is coming in the process of execution. Please help.
clear;clc;close all
S = load('DQS.mat');
F = fieldnames(S);
for n = 1:numel(F)
F1 = fieldnames(S.(F{n}));
F11{n} = F1;
% allocate the variables
nn = cell(numel(F1),1);
PArea = cell(numel(F1),1);
t_max = cell(numel(F1),1);
for n1 = 1:numel(F1)
t = S.(F{n}).(F1{n1}).cc.t;
t_max{n1} = max(t);
nn{n1} = F1{n1};
output{n1} = cumtrapz(t);
NF = @(p,q) max(output{n1}(t<=q)) - min(output{n1}(t>=p));
PArea{n1} = NF(5, 6);
end
T_max{n} = t_max;
tt = cell2mat(T_max{1,n}(:,end));
td{n} = tt./max(tt);
cumulat{n} = PArea;
[R{n},P{n}] = corrcoef(td,cumulat); %correlation coeff. calculation
end
Error using corrcoef
Cannot compute p-values for complex inputs.

댓글 수: 1

Jonas
Jonas 2022년 6월 23일
can you please edit your question, format your code as code and press the compile button (green rectangle), this way we can see what is happening

댓글을 달려면 로그인하십시오.

 채택된 답변

Mathieu NOE
Mathieu NOE 2022년 6월 23일

0 개 추천

hello
seems to me the code could be made simpler, why the need to make all this cell acrobatics ?
clear;clc;close all
S = load('DQS.mat');
F = fieldnames(S);
for n = 1:numel(F)
F1 = fieldnames(S.(F{n}));
F11{n} = F1;
% allocate the variables
nn = cell(numel(F1),1);
for n1 = 1:numel(F1)
t = S.(F{n}).(F1{n1}).cc.t;
t_max(n1) = max(t);
nn{n1} = F1{n1};
output{n1} = cumtrapz(t);
NF = @(p,q) max(output{n1}(t<=q)) - min(output{n1}(t>=p));
PArea(n1) = NF(5, 6);
end
td = t_max./max(t_max);
cumulat = PArea;
[R{n},P{n}] = corrcoef(td,cumulat); %correlation coeff. calculation
end

댓글 수: 8

Josh
Josh 2022년 6월 23일
DQS structure array has two fields: 'xc1' & 'xc2'. 'xc1' has three fields-'c1','c2', & 'c3' whereas xc2 has two fields-'c1', and 'c2'. Now, the output of the modified code is for field 'xc2' with the thrird row of field 'xc1' as it is not overwritten. The cell operations in the previous code was utilized to keep both fields i.e. 'xc1' & 'xc2'. Therefore this modified code is not resolving the earlier issue. Please reconsider.
In your original code you use corrcoef with cell arrays as inputs
that does not work, corrcoef needs numerical array
clear;clc;close all
S = load('DQS.mat');
F = fieldnames(S);
for n = 1:numel(F)
F1 = fieldnames(S.(F{n}));
F11{n} = F1;
% allocate the variables
nn = cell(numel(F1),1);
PArea = cell(numel(F1),1);
t_max = cell(numel(F1),1);
for n1 = 1:numel(F1)
t = S.(F{n}).(F1{n1}).cc.t;
t_max{n1} = max(t);
nn{n1} = F1{n1};
output{n1} = cumtrapz(t);
NF = @(p,q) max(output{n1}(t<=q)) - min(output{n1}(t>=p));
PArea{n1} = NF(5, 6);
end
T_max{n} = t_max;
tt = cell2mat(T_max{1,n}(:,end));
td{n} = tt./max(tt);
cumulat = PArea;
[R{n},P{n}] = corrcoef(td{n},cell2mat(cumulat)); %correlation coeff. calculation
end
In terms of results R{n},P{n} both suggestions gives the same results
Josh
Josh 2022년 6월 23일
As the outputs are mostly 'NaN' values, it seems the inputs to 'corrcoef' function are not the numerical values of variables i.e. 'td' & 'cumulat'. Please reconsider & see if it can be resolved.
they are numerical
first iteration (n= 1)
both inputs are numerical :
K>> td{n} =
0.8333
0.7500
1.0000
K>> cell2mat(cumulat) =
5.5000
5.5000
5.5000
result is :
corrcoef(td{n},cell2mat(cumulat)) =
1 NaN
NaN NaN
and for the second iteration , same comments
K>> td{n} =
0.7500
1.0000
K>> cell2mat(cumulat) =
5.5000
5.5000
K>> corrcoef(td{n},cell2mat(cumulat)) =
1 NaN
NaN NaN
Josh
Josh 2022년 6월 23일
Thanks for your kind reconsiderations.It is resolved.
OK
all the best

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기

질문:

2022년 6월 23일

댓글:

2022년 6월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by