Creating a multidimensional correlation matrix from excel file

7 views (last 30 days)
Anthony Tan on 14 May 2021
Answered: Pratyush Roy on 24 May 2021
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

Pratyush Roy on 24 May 2021
Hi Anthony,
The following code might be helpful to generate a set of correlation matrices arranged in a correlation volume:
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!

R2018b

Community Treasure Hunt

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

Start Hunting!