split cell (containing strings) into cell according to the value of C (column)
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Alberto Acri
il 20 Set 2023
Commentato: Dyuman Joshi
il 21 Set 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 Commenti
Risposta accettata
Dyuman Joshi
il 20 Set 2023
Modificato: Dyuman Joshi
il 20 Set 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 Commenti
Dyuman Joshi
il 21 Set 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)'
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Cell Arrays in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!