split cell (containing strings) into cell according to the value of C (column)
1 view (last 30 days)
Show older comments
Alberto Acri
on 20 Sep 2023
Commented: Dyuman Joshi
on 21 Sep 2023
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 Comments
Accepted Answer
Dyuman Joshi
on 20 Sep 2023
Edited: Dyuman Joshi
on 20 Sep 2023
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 Comments
Dyuman Joshi
on 21 Sep 2023
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)'
More Answers (0)
See Also
Categories
Find more on Characters and Strings 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!