Creating a struct with means and standard deviations

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:
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.

 Akzeptierte Antwort

Chris
Chris am 16 Sep. 2022
Bearbeitet: Chris am 16 Sep. 2022
It looks like you want an array of structs. Use strings or uint64 for the IDs
S = struct('id',uint64(123456),'mean',rand(3),'stdev',rand(1,3))
S = struct with fields:
id: 123456 mean: [3×3 double] stdev: [0.7411 0.1904 0.5026]
% 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
S = 1×4 struct array with fields:
id mean stdev
% Retrieve data
x = S(3).stdev
x = 1×3
0.6080 0.0728 0.3028
y = [S(1:2).id]
y = 1×2
123456 614065544

5 Kommentare

Hi, thank you for your help! This is great, but the main issue I was having was trying to figure out how to access my list of subjects into a different table and the way you answered the question, it actually avoided that problem I was running into.
In essence, my current table has the subject list repeating down a column and I was hoping to get just one line per subject. Here is a made up example (the subjects repeat about 10 times, this table only shows 3 to avoid creating a massive table)
This is the code I was running that is getting me a really long list of subjects
EventData = readtable('Event_1_2.xlsx');
Updating = struct;
for s=1:length(EventData.UserID)
Updating(s).subjectID = EventData.UserID(s);
... %code mean + std. dev. script goes here
end
I'm not entirely clear on what you're trying to do and how struct is involved, but the illustration of SubID going from repeated values to unique values makes me think you may want to use unstack on the table of results.
BA
BA am 16 Sep. 2022
I don't think I would be able to do a table.
What I'm trying to get is a table within a table
Column 1 = UserIDs
Column 2 = Each Table w/ mean of event_x_y
Column 3 = Each Table w/ std. of event x_y
The parts of the table are color coded here. So the "MEAN DOUBLE" table in green is essentially a blown up version of the table at the top (1,2).
The red table "STANDARD DEVIATION DOUBLE" is a blown up version of Row 3 Column 3 (row numbers don't include row 1 w/ variable names in this case).
So each value that you see in these smaller tables at the bottom are the mean/std. dev. of event_1_a and so on for the others.
Chris
Chris am 16 Sep. 2022
Bearbeitet: Chris am 16 Sep. 2022
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
BA
BA am 16 Sep. 2022
Thank you! That works. Appreciate you for helping me out!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2022a

Tags

Gefragt:

BA
am 16 Sep. 2022

Kommentiert:

BA
am 16 Sep. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by