How to Split Data Into Groups Based on Number of Elements in Cell?
7 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
Ian Adler
2016-6-9
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!