Creating a multidimensional correlation matrix from excel file

조회 수: 6 (최근 30일)
Anthony Tan
Anthony Tan 2021년 5월 14일
답변: Pratyush Roy 2021년 5월 24일
Hi, I wish to create a multidimensional correlation matrix, C, involving three variables for time period ranging from i = 1 to n, i.e. (3:3:i), with i = 1:n
I have a list of correlation coefficients between var1 and var2, between var 2 and var3 and between var1 and var3 for each time period, organized in excel file, as three separate columns.
I wish to find out how to input the data files into matlab, and write a command that generates the matrix C:
C = (:,:,1) = [1 corr(var1,var2) corr(var1,var3); corr(var2,var1) 1 corr(var2,var3) ; corr(var3,var1) corr(var3,var2) 1) [for i=1]
C = (:,:,2) = [1 corr(var1,var2) corr(var1,var3); corr(var2,var1) 1 corr(var2,var3) ; corr(var3,var1) corr(var3,var2) 1) [for i=2]
...
C = (:,:,n) = [1 corr(var1,var2) corr(var1,var3); corr(var2,var1) 1 corr(var2,var3) ; corr(var3,var1) corr(var3,var2) 1) [for i=n]
Thank you

답변 (1개)

Pratyush Roy
Pratyush Roy 2021년 5월 24일
Hi Anthony,
The following code might be helpful to generate a set of correlation matrices arranged in a correlation volume:
T = readtable('corr.xlsx','PreserveVariableNames',true);
T2 = T(:,~ismember(T.Properties.VariableNames, {'time = i'}));
tableArray = table2array(T2);
[rowNum,colNum] = size(tableArray);
corrVol = ones(colNum,colNum,rowNum);
for i=1:rowNum
for j=1:colNum
for k = 1:colNum
if ((j==2)&(k==1) | (j==1)&(k==2))
corrVol(j,k,i) = tableArray(i,1);
elseif ((j==3)&(k==1) | (j==1)&(k==3))
corrVol(j,k,i) = tableArray(i,2);
elseif ((j==3)&(k==2) | (j==2)&(k==3))
corrVol(j,k,i) = tableArray(i,3);
end
end
end
end
Here we are using readtable command to read the excel file and converting the table into array using table2array. We are removing the column containing the dates using the ismember function.
Hope this helps!

카테고리

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

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by