split cell (containing strings) into cell according to the value of C (column)
2 次查看(过去 30 天)
显示 更早的评论
Hi! Is there a way to split cell 'a4' (containing strings) into cell 'a4_new'?
In particular I would like to divide the columns of 'a4' according to the value of C (for example C=6, but it must be valid for C=1:10).
Col = 6;
load a4 %in
load a4_new %out
0 个评论
采纳的回答
Dyuman Joshi
2023-9-20
编辑:Dyuman Joshi
2023-9-20
load a4 %in
load a4_new %out
%Checking the values of the variables
a4
a4_new
Col = 6;
%Split according to the multiples of Col
n = numel(a4);
%Groups to divide the data into
idx = [repelem(Col,1,floor(n/Col)) rem(n,Col)]
out = mat2cell(a4,1,idx)'
2 个评论
Dyuman Joshi
2023-9-21
Yes, the empty row will arise when the number of elements of A is perfect divisible by Col (as the reminder will be 0). I seem to have overlooked it last night.
In that case -
load('a4.mat')
n = numel(a4)
%Split according to the multiples of Col
Col = 3;
%Groups to divide the data into
idx = [repelem(Col,1,floor(n/Col)) rem(n,Col)]
%% Simply delete the 0 value
idx(idx==0) = [];
out = mat2cell(a4,1,idx)'
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!