split cell (containing strings) into cell according to the value of C (column)
조회 수: 1 (최근 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!