Creating a struct with means and standard deviations
조회 수: 2 (최근 30일)
이전 댓글 표시
I'd like to create a script that loops through multiple subjects and creates a new structure with doubles in it.
First Column of Master Table: I want the SubjectID but I only want it once, I don't want it repeating like it is in the dataset I've attached.
Second Column of Master Table: I want it to lead to a struct with the means of each of the six events. So in the new struct, I want the first column of the first row to be for event 1_a, second column first row event 2_a, etc.
Third Column of Master Table: I want it to lead to a struct with the standard deviation of each of the six events.
Here's an example of what I want it to look like:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1127180/image.png)
The examples of mean and standard deviation doubles are only for one subject. I'd like to loop this for every subject in the data table to create a master struct.
댓글 수: 0
채택된 답변
Chris
2022년 9월 16일
편집: Chris
2022년 9월 16일
S = struct('id',uint64(123456),'mean',rand(3),'stdev',rand(1,3))
% Add new data
for idx = 2:4
S(idx).mean = rand(3);
S(idx).stdev = rand(1,3);
S(idx).id = uint64(randi(1e9));
end
S
% Retrieve data
x = S(3).stdev
y = [S(1:2).id]
댓글 수: 5
Chris
2022년 9월 16일
편집: Chris
2022년 9월 16일
Unstack sounds useful, but I'm unfamiliar with it and man, I need to learn some table stuff.
@BA How about something like this?
EventData = readtable('Event_1_2.xlsx');
vars = T.Properties.VariableNames;
[u,~,ic] = unique(EventData.UserID);
for idx = 1:numel(u)
Updating(idx).ID = u(idx);
T = EventData(ic==idx,2:end);
Updating(idx).stdev = array2table(std(T{:,:}),'VariableNames',vars);
Updating(idx).mean = array2table(mean(T{:,:}),'VariableNames',vars);
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!