splitting column in table without creating new variables
2 次查看(过去 30 天)
显示 更早的评论
I have continous data with 100 datapoints for each variable in each trial (100 datapoints of a time series). Every trial of one variable is added in the same column of the table with a second column specifying the trial number and a third column specifying the session number.
I want to average the datapoints of the different trials now, always averaging each data point with theoretically the same index if they were seperate vectors, ergo datapoint 1 of all trials, then datapoint 2 of all trials and so on. Then I want to save the average and mean as columns into a new table.
Is there a way to do this with indexing in the table directly and without creating individual vectors for each trial again?
So far I have been doing it this way, but I imagine there must be a more efficient way of doing it as I have many variables and trials and participants and it would take up a lot of space writing it out like this.
x1 = alltrials.Var1(alltrials.TrialNo==1);
x2 = alltrials.Var1(alltrials.TrialNo==2);
x3 = alltrials.Var1(alltrials.TrialNo==3);
average = table(x1, x2, x3);
average.mean = mean([average.x1 average.x2 average.x3],2);
average.median = median([average.x1 average.x2 average.x3],2);
avgtrials.Var1_mean = average.mean;
avgtrials.Var1_median = average.median;
0 个评论
采纳的回答
Duncan Po
2021-6-9
Unstack may be the function you need to convert your table into a column for each trial, and then use braces indexing to convert the table into an array. See this example:
allTrials = table(rand(12,1),repelem((1:3)',4,1), repmat((1:4)',3,1), 'VariableNames', {'Var1', 'TrialNo', 'SessionNo'});
head(allTrials,6)
ans =
6×3 table
Var1 TrialNo SessionNo
_______ _______ _________
0.70936 1 1
0.75469 1 2
0.27603 1 3
0.6797 1 4
0.6551 2 1
0.16261 2 2
t = unstack(allTrials, "Var1", "TrialNo", "VariableNamingRule", "preserve");
t.mean = mean(t{:,2:end},2);
t.median = median(t{:,2:end},2);
head(t)
ans =
4×6 table
SessionNo 1 2 3 mean median
_________ _______ _______ _______ _______ _______
1 0.70936 0.6551 0.95974 0.77474 0.74205
2 0.75469 0.16261 0.34039 0.41923 0.37981
3 0.27603 0.119 0.58527 0.32676 0.30139
4 0.6797 0.49836 0.22381 0.46729 0.48283
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!