How to Split Data Into Groups Based on Number of Elements in Cell?

5 views (last 30 days)
Brian Tomblin
Brian Tomblin on 9 Jun 2016
Edited: Brian Tomblin on 9 Jun 2016
I am incredibly new to Matlab, so any help would be appreciated!
After running a program, I am given a long row of cells with between 1 and 40 individual values within each cell. Cells that have 7 or less numerical elements need to go through a different function than cells that have more than 7 numbers. There are no categories defined for the cells.
What can I write to have Matlab, after receiving the data in a 1xN cell array, determine how many elements are in each cell and separate the cells to then be handled differently?
Thanks.

Answers (1)

Ian Adler
Ian Adler on 9 Jun 2016
creating an example cell
a=cell(2);
a(1,1)={54};
a(2,1)={20155};
a(1,2)={12};
a(2,2)={15485};
Converting it to a table so we can use it
b=cell2table(a);
Declaring some variables for later user
x=1;
y=1;
for x=1:height(b) %choose either height or width whichever is longer
if (numel(num2str(b.a1(x,y))) <= 2) == 1
if (the number of characters in (the string version of (cell b.column a1))) is less than 2) is TRUE
disp([num2str(x) ',' num2str(y) ' is <= 2 numbers']);
%display ([the character version of (x), the a ',', etc])
end
if (numel(num2str(b.a1(x,y))) >= 3) == 1
repeat for cell b. column a1 but if its greater than 3
disp([num2str(x) ',' num2str(y) ' is >= 3 numbers']);
end
if (numel(num2str(b.a2(x,y))) <= 2) == 1 %repeat for cell b column a2
disp([num2str(x) ',' num2str(y) ' is <= 2 numbers']);
end
if (numel(num2str(b.a2(x,y))) >= 3) == 1 %again as above
disp([num2str(x) ',' num2str(y) ' is >= 3 numbers']);
end
end
end
My full code so you can copy and paste and play around wit it
a=cell(2);
a(1,1)={54};
a(2,1)={20155};
a(1,2)={12};
a(2,2)={15485};
%creating an example cell
b=cell2table(a);
%converting it to a table
x=1;
y=1;
for x=1:height(b) %choose either height or width whichever is longer
if (numel(num2str(b.a1(x,y))) <= 2) == 1 %if (the number of characters in (the string version of (cell b.column a1))) is less than 2) is TRUE
disp([num2str(x) ',' num2str(y) ' is <= 2 numbers']); %display ([the character version of (x), the a ',', etc])
end
if (numel(num2str(b.a1(x,y))) >= 3) == 1 %repeat for cell b. column a1 but if its greater than 3
disp([num2str(x) ',' num2str(y) ' is >= 3 numbers']);
end
if (numel(num2str(b.a2(x,y))) <= 2) == 1 %repeat for cell b column a2
disp([num2str(x) ',' num2str(y) ' is <= 2 numbers']);
end
if (numel(num2str(b.a2(x,y))) >= 3) == 1 %again as above
disp([num2str(x) ',' num2str(y) ' is >= 3 numbers']);
end
end
  1 Comment
Brian Tomblin
Brian Tomblin on 9 Jun 2016
So this determines the amount of numbers in a value for each cell (if I'm running it correctly on my end). What I'm looking for is to determine the amount of values in a cell, not the size of the number.
Ex. {1,1} = (1,2,1,2,2,3,1) {1,2} = (1,2,3,2,1,2,3,1,3,1,2,3,1,2) I need cells that have 7 or less values (like {1,1}) to be differentiated from cells that have more than 7 values (like {1,2}) so I can, for example, take the mean in cells with 7 or less while ignoring the cells with more than 7.
Does your example work for that idea?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!