- Show us sample data
- Show how you handle it.
- Show the error if it failed
group numbers in cells
5 views (last 30 days)
Show older comments
Hi,
I have a 1*5 cell and each of them contains three columns and a variable numer of rows.
In this case, the first column of each cell represents my temperture and the other two columns contain data. As mentioned, each cell has a different number of rows, i.e., the temperatures are not lying in the same row number.
Here what I would like to do: find the same temperature in each cell and group them in a unique array of data, along to the rest of the data, i.e. the content of the other two columns.
Thanks for your help
2 Comments
Yongjian Feng
on 6 Nov 2021
Try to write something first, and we can help you to improve.
Accepted Answer
Dave B
on 7 Nov 2021
I think you're asking how to go from cells that have a sort of group-id in the first column to recombined cells that all have that value in the first column?
Here's one idea (this felt a little convoluted but I think it does the trick):
- combine everything into one matrix
- sort it
- use findgroups to convert those group identies into consecutive integers
- use histcounts to get counts (i.e. how many values in each new cell)
- use mat2cell to convert it back to a cell
d1=load('data1');
d2=load('data2');
c = {d1.RH d2.RH};
alldata=cell2mat(c(:));
alldata=sortrows(alldata,1);
grpid=findgroups(alldata(:,1));
res=mat2cell(alldata, histcounts(grpid,'BinWidth',1), 3)';
res{1}
res{2}
res{3}
3 Comments
Dave B
on 7 Nov 2021
I think you want a table, because this looks like a table (a bunch of rows with heterogenous data). I'd question whether you really want to split it up into separate cells, perhaps it's better to keep everything in one table and just generally plan on being able to index out a particular temperature (that's seems like a more MATLABby approach IMO).
d1=load('data1');
d2=load('data2');
c = {d1.RH d2.RH};
t=table;
for i = 1:numel(c)
% Note: if you only have your cell array for the purposes of loading
% files, just load your files here (i.e. loop over file names rather
% than putting them in a cell in the first place!)
this_t=array2table(c{i},'VariableNames',["Temperature" "Thing1" "Thing2"]);
this_t.Source(1:height(this_t))="data" + i;
t=[t;this_t];
end
t=sortrows(t,["Temperature" "Source"]);
t=movevars(t,'Source',"Before",'Temperature'); % order doesn't really matter but this looks prettier
head(t)
% Suggest you stop here, you can always iterte over temperatures or data
% sources etc. So why bother with splitting it up? It will make nothing
% easier and some things harder...
% if you really want to split it up:
grps = unique(t.Temperature);
separates = cell(1,numel(grps));
for i = 1:numel(grps)
separates{i} = t(t.Temperature==grps(i),:);
end
separates{1}
separates{2}
separates{3}
More Answers (0)
See Also
Categories
Find more on Database Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!