group numbers in cells

5 views (last 30 days)
Alessandro
Alessandro on 6 Nov 2021
Commented: Alessandro on 8 Nov 2021
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
Yongjian Feng on 6 Nov 2021
Try to write something first, and we can help you to improve.
  1. Show us sample data
  2. Show how you handle it.
  3. Show the error if it failed
Alessandro
Alessandro on 6 Nov 2021
please find attached part of the code.
By loading the two .m files, I have 1*2 cell.
What I would like is a new series of 1*N cells that contains:
1) 11 45.88 0.7510
2) 12 65.91 0.92
12 43.57 0.16
3) 13 ... etc
4) 15 ...
The 'grouping' variables are the numbers in the first column.
I have been trying different ways but I have no clue how to handle it.
Could you please give advice on this?
files = dir('data*.mat');
table = struct2table(files);
sample_numfiles = length(files);
sample_data_rh = cell(1, sample_numfiles);
for k = 1:sample_numfiles
sample_data_rh{k} = load(files(k).name);
rh_data{k} = (sample_data_rh{k}.RH(:,:));
end

Sign in to comment.

Accepted Answer

Dave B
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}
ans = 1×3
11.0000 45.8899 0.7510
res{2}
ans = 2×3
12.0000 65.9147 0.9272 12.0000 43.5758 0.1654
res{3}
ans = 2×3
13.0000 62.5894 0.3408 13.0000 40.1022 0.0644
  3 Comments
Dave B
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)
ans = 8×4 table
Source Temperature Thing1 Thing2 _______ ___________ ______ ________ "data2" 11 45.89 0.75101 "data1" 12 65.915 0.92723 "data2" 12 43.576 0.16542 "data1" 13 62.589 0.34078 "data2" 13 40.102 0.064415 "data1" 15 56.666 0.26838 "data2" 15 36.772 0.49241 "data1" 17.5 50.386 0.62024
% 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}
ans = 1×4 table
Source Temperature Thing1 Thing2 _______ ___________ ______ _______ "data2" 11 45.89 0.75101
separates{2}
ans = 2×4 table
Source Temperature Thing1 Thing2 _______ ___________ ______ _______ "data1" 12 65.915 0.92723 "data2" 12 43.576 0.16542
separates{3}
ans = 2×4 table
Source Temperature Thing1 Thing2 _______ ___________ ______ ________ "data1" 13 62.589 0.34078 "data2" 13 40.102 0.064415
Alessandro
Alessandro on 8 Nov 2021
Super!!
Thank you so much for your help, Dave.

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!