Sorting Data into a Table
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
I have a dataset that consists of a single column with x amount of rows (only containing, 1, 2, 3s). I want to take this column and form a table and sort the 1s, 2s, and 3s into different columns. I know I will likely need if statements but not sure where to start. I want to form a table with 3 columns and the 1s sorted into one columns, 2s in another, and so on. Any help would be greatly appreciated!
Thank you in advance!!
4 Kommentare
dpb
am 7 Okt. 2022
Bearbeitet: dpb
am 7 Okt. 2022
No! You did NOT understand @Walter Roberson's Q? -- what you show is what you CANNOT do in a MATLAB table or ordinary array; MATLAB is NOT a spreadsheet and does not support "ragged" arrays (except by cell arrays which can hold anything in a given cell) and even there the cell array itself must be rectangular.
In one variable other than a cell array, you can only simulate the result by padding each column to the same length with NaN or some other indicator value.
Antworten (1)
dpb
am 7 Okt. 2022
Bearbeitet: dpb
am 7 Okt. 2022
% make up some data
N=randi(5,3,1);
x=[];for i=1:numel(N),x=[x;i*ones(N(i),1)];end
x=x(randperm(numel(x)))
% the engine (cell array solution)
[u,~,ic]=unique(x);
for i=1:numel(u)
C{i}=x(ic==u(i));
end
C{:}
% padded array solution
clear C
N=max(histc(ic,u)); % max number of any value
C=nan(N,numel(u)); % preallocate 2D array prefilled to NaN for missing
for i=1:numel(u)
ix=(ic==u(i));
C(1:sum(ix),i)=x(ix);
end
C
% convert to table
tC=array2table(C,'variablenames',{'1:REM','2:NREM','3:WAKE'})
You don't give enough information to know what is end result here, but one kinda' presumes there is going to be an analysis of these data by the above variable values. If so, probably the best way to store the data is in a table with the conditions as above as variables -- turning the 1:3 vector into a categorical variable with those names as the categories would likely be a good first step.
Then, analyses could follow with groupsummary or the other tools for handling grouping variables in tables. Look up the background information on processing data in tables; particularly the "split apply" workflow; it'll be eye opening of capabilities.
1 Kommentar
dpb
am 7 Okt. 2022
Carrying on from the above observation -- given the names associated with the various indices, one presumes the multiple instances of each are associated with different subjects and/or monitored sleep periods and there would be various other observations associated with each.
In that case it would make much more sense for analyses to do something more like
% make up some data
N=randi(5,3,1);
x=[];for i=1:numel(N),x=[x;i*ones(N(i),1)];end
N=numel(x);
x=x(randperm(N));
STAGES=categorical({'REM','NREM','WAKE'});
stage=STAGES(x).';
id=categorical("Subject"+randi(5,numel(x),1));
tStart=timeofday(datetime(0,0,0,randi([20 23],N,1),randi([0 59],N,1),0));
tEnd=tStart+minutes(randi([60 8*60],N,1));
tData=table(id,stage,tStart,tEnd,tEnd-tStart,'variablenames',{'Subject','Stage','Initiate','Terminate','Duration'})
There could, of course, be all kinds of other associated data as additiona fields...but, the beauty of such an arrangement is in being able to do things like
groupsummary(tData,{'Subject'})
groupsummary(tData,{'Subject','Stage'},@mean,{'Initiate','Terminate','Duration'})
Siehe auch
Kategorien
Mehr zu Tables finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!