selection of one parameter for correlation coefficient

조회 수: 1 (최근 30일)
Ismita
Ismita 2022년 4월 29일
답변: Abhishek Chakram 2023년 9월 22일
Suppose, I have 100 rows of P and Q, and 20 rows of data make a group. Average of each group gives new set of data points (total 5 for each). I want to calculate correlation of coefficient for each group. But it gives 2x2 matrics. how can I select one value for each data 20 row?
P = [1:100];
Q = [1:100];
index = 20;
new_group = zeros(5,3); % average of each 20 row of P (=P_av), Q (=Q_av), and correlation of coefficient for each 5 groups of P and Q
A = randn(10,1);
B = randn(10,1);
R = corrcoef(A,B)
Result: R = 2×2
1.0000 0.4518
0.4518 1.0000 %
%how to select one (here 0.4518) for each group?
Thanks a lot in advance.

답변 (1개)

Abhishek Chakram
Abhishek Chakram 2023년 9월 22일
Hi Mst Ismita Tasnim,
It is my understanding that you are facing difficulty in writing the code for selecting a particular parameter for correlation coefficient. Here’s an example for the same:
P = [1:100];
Q = [1:100];
index = 20;
new_group = zeros(5,3); % average of each 20 row of P,Q,and the correlation coefficient for each 5 groups of P and Q
% Loop through each group
for i = 1:5
% Select the rows for the current group
start_index = (i-1)*index + 1;
end_index = i*index;
P_group = P(start_index:end_index);
Q_group = Q(start_index:end_index);
% Calculate the average of P and Q for the current group
P_av = mean(P_group);
Q_av = mean(Q_group);
% Calculate the correlation coefficient for the current group
R = corrcoef(P_group, Q_group);
correlation_coefficient = R(1, 2); % Select the value at (1, 2) position
% Store the results in the new_group array
new_group(i, :) = [P_av, Q_av, correlation_coefficient];
end
Best Regards,
Abhishek Chakram

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by