How do I keep track of string values relating to columns before I shove everything into a table?
4 次查看(过去 30 天)
显示 更早的评论
Dear all,
I posted on this before, but having included the whole script, I think it was a big ask for anyone to bother with it. So I've attached below a version with plenty of stuff cut out, just to give a general idea, and a more detailed explanation after the script.
I have a code that works, but something is missing. I do with that data what I want it to, but I have to keep track of names. The script uses 4 columns of random data. The code essentially find the coherence between the variables -that is, between each combination of the variables. With four variables, there's 6 unique combinations. The coherence is just a measure of linear relation at different frequency, so it's the frequency domain counterpart of cross-correlation. Because I have small sample sizes, I do bootstraps (block bootstraps, to be exact). The output is the 14x6 array meancoh, which is the mean across x number of bootstrap for each pair, each combination. The function inside the loop operates on two variables at the time. Now, once I apply this to real data, say 20-30 variables at the time, there will be many more combinations than 6. So I need to be able to keep track of things. Say the four columns are named (i.e. the content of headers is): a, b, c and d. I need to associate to each column of meancoh a title to the tune of 'mean of bootstrapped coherence between a and b', 'mean of bootstrapped coherence between a and c', etc . How on earth do I do that? Any help/tips/suggestions would be greatly appreciated. More below:
% load data
clear
[~,~,raw] = xlsread( '2series.xlsx',8) ;
headers = raw(1,1:4);
data = raw(2:260,1:4) ; % Cell array mixed
data = cell2mat( data ) ; % Numeric array.
[nRows, nCols] = size( data ) ;
combinations =nchoosek(1:nCols,2); %returns a matrix containing all possible combinations of the elements of vector 1:ncols taken 2 at a time
%if newrandata is 3 columns, x returns all combinations of 1 to 3
n1=size(combinations,1); %returns the # of combinations, i.e. the # of rows in x
B=zeros(TT2,L*L);
gg=1;
meancoh=zeros(TT2,n1);
for ii=1:(nCols-1) %so this is from 1 to 3
for kk = ii+1:nCols %and this is from 2 to 4, so no duplicates
p=1;
for i=1:L % for all L realisations of the bootstrap
..
end
here for the first (ii) column of data I calculate the 1st of L bootstraps
and I name it xc
for g=1:L % here a nested loop that calculates L bootstrap for kk columns and yields something called
yc
end
B(:,p)= mscohere (xc(:,i),yc(:,g));
%So this calculates the coherence between L bootstraps of column 1 with L bootstraps of columns 2 to 4
p=p+1;
end
end
meancoh(:,gg)=mean(B,2);
gg=gg+1;
end
end
Now, if I take the titles of my original data from excel, and turn them into character arrays, with something like this:
titles=cell2mat(headers(1,whatever index));
AND if I was able to keep track of the string value associated with xc and yc, I would be set, since
table2series=array2table(meancoh,'Variablenames',{titlesa,titleb,titlesc,titled,titled,titlef}) would give me exactly what I want, i.e. the bits of strings that I need to put together to name each column of meancoh, like so:
something= strcat({mean of bootstrapped coherence between} Name of xc, { and }, Name of yc)
I have the feeling I have to start with combinations. So far, I’ve used it just to work out how MANY combinations there are (n1, to give the proper dimensions to array B).
I think somehow I have to assign indices to characters in titles, then associate them to xc and yc in the loop-but how? I’m stuck.
1 个评论
jonas
2018-11-27
I don't understand what you're asking. Given a table of 3 columns, let's call them a, b and c, then you have three unique pairs: ab, ac and bc. Do you want to do keep track of the combinations and associate each result with the corresponding pair identifier?
I'd start by importing the data by readtable instead of the inconvenient raw format. That way at least each table column will be associated with a variable name. The rest should not be very difficult, unless I completely misunderstood the question.
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!