CircAnalyzer
CircAnalyzer

Reputation: 704

MATLAB partition cell array

I have a cell array such that it is 128 characters long, i.e

c = {'1......128'}

What I'd like to do is break it up into chunks of 8, starting from the left, then put each 8-chunk piece into a new cell array. What is the easiest way to do this?

Upvotes: 1

Views: 73

Answers (3)

Vahe Tshitoyan
Vahe Tshitoyan

Reputation: 1439

You can do it with one line

mycell = repelem('a', 128); % creating the cell

newcells = cellstr(reshape(mycell{:},8,[])'); % cells with 8 characters each

if your cell is just 1x1 with 128 characters.

Upvotes: 4

Loamsiada
Loamsiada

Reputation: 454

What about the following?

res = cell(1,16);
for ii=0:15
    res{ii} = c{1}((1:8)+ii*8);
end

Upvotes: 0

Flynn
Flynn

Reputation: 269

valS = 1;
valE = 8;

for ii=1:(128/8)
   newC{ii,:} = c{valS:valE};
   valS = valE + 1; % after first loop valS = 9 ...
   valE = valE + 8; % after first loop valE = 16 ...
end

You could also use eval if you wanted to separate newC into different variables completely

Upvotes: 1

Related Questions