split cell (containing strings) into cell according to the value of C (column)
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Alberto Acri
am 20 Sep. 2023
Kommentiert: Dyuman Joshi
am 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 Kommentare
Akzeptierte Antwort
Dyuman Joshi
am 20 Sep. 2023
Bearbeitet: Dyuman Joshi
am 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 Kommentare
Dyuman Joshi
am 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)'
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!