Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

machine learning - How to calculate and save correlation coefficients in matlab?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Looking at the corrcoef documentation you can see that the output is a 2x2 matrix.

The easiest way then to save all the coefficients would be to initialize your matrix C outside of the loop and then just assign values at each iteration. Perhaps something like

C = zeros(2, 2, length(window:lenght(M)-window));
for i = window:length(M) - window
    C(:, :, i) = corrcoef(M(i-window/2:i+window/2),V(i-window/2:i+window/2));
end

Even better, you can also see that the output of the corrcoef function is structured like this:

[1, c1;
 c2, 1];

where c1 and c2 are the actual coefficients. So you could also do:

C = zeros(length(window:lenght(M)-window), 2);
for i = window:length(M) - window
    temp = corrcoef(M(i-window/2:i+window/2),V(i-window/2:i+window/2));
    C(i, 1) = temp(1, 2); % "c1"
    C(i, 2) = temp(2, 1); % "c2"
end

EDIT TO ANSWER TO COMMENT

To implement a sliding window with any size and with any overlap, you might want to change the indexing:

wdwSize = 2;
overlap = 0.5;

C = zeros(length(1:wdwSize*overlap:length(A)-wdwSize*overlap), 2);

for i = 1:wdwSize*overlap:length(A)-wdwSize*overlap
    window = i:i+wdwSize - 1;
    temp = corrcoef(M(window), V(window));
    C(i, 1) = temp(1, 2); % "c1"
    C(i, 2) = temp(2, 1); % "c2"
end

In your case wdwSize*overlap is an integer, but you might want to be careful for a general combination of window size and overlap.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...