Extracting from cell arrays using a nested loop

I have a 1x250 cell array. Each cell contains an 87001x16 matrix. I want to extract all of the first column from cell 1, column 1 from cell 2, column 1 from cell 3, ... column 1 from cell 250, to create a 87001x250 matrix. And I want to do this with all 16 columns, creating a separate 1x16 cell array, with each cell containing an 87001x250 matrix.
When I run the following code, I get a 1x16 cell array, with each cell being the first 16 columns of the 250th cell:
for k = 1:250
Data{k} = Matrix(zapTime(k)-12000:zapTime(k)+75000, 2:end); % Yields 1x250 cell array, with 87001x16 matrices.
for c = 1:16
subData{c} = Data{k}(:,c); % Creates 1x16 cell array, with first 16 columns from Data cell #250.
end
end
When I run this code, I get a 1x16 cell array, with each cell being the matrices from the first 16 cells.
for k = 1:250
Data{k} = Matrix(zapTime(k)-12000:zapTime(k)+75000, 2:end); % Yields 1x250 cell array, with 87001x16 matrices.
for c = 1:16
subData{c} = Data(:,c); % Creates 1x16 cell array, with first 16 matrices from Data cell array.
end
end
I'm able to code things separately just fine when I use the following code:
subData{k} = Data{k}(:,1);
subData{k} = Data{k}(:,2);
subData{k} = Data{k}(:,3);
...
subData{k} = Data{k}(:,16);
But certainly a nested loop would be more efficiant. When I try this...:
for c = 1:16
subData{k} = Data{k}(:,c);
end
...I get 250 cells of the data from columns 16.
Please help! Thank you!

Answers (2)

Stephen23
Stephen23 about 1 hour ago
Edited: Stephen23 about 1 hour ago
The issue is that subData{c} is being overwritten on each iteration of k, whereas you need to index into both dimensions (c and k) simultaneously, building up each 87001x250 matrix column by column.
Instead use subData{c}(:,k) to assign the k-th column of each sub-matrix:
% Bild Data as before
for k = 1:250
Data{k} = Matrix(zapTime(k)-12000:zapTime(k)+75000, 2:end);
end
% Pre-allocate and fill subData:
subData = repmat({nan(87001,250)},1,16); % preallocate
for k = 1:250
for c = 1:16
subData{c}(:,k) = Data{k}(:,c); % k-th column of the c-th output matrix
end
end
Here is another approach using cellfun and cell2mat (this won't be faster, just more compact code):
subData = cell(1,16);
for c = 1:16
subData{c} = cell2mat(cellfun(@(x) x(:,c), Data, 'UniformOutput',false));
end
And for completeness, a solution all on one line (warning: creates a large intermediate 3D array!):
subData = reshape(num2cell(permute(cat(3, Data{:}), [1,3,2]), [1,2]), 1,16);

2 Comments

Thank you so much! Do you recommend one of these methods over the others (for efficiency or speed)? I'm currently working with only part of our dataset for simplicity/time, but in the future I'll be working with larger datasets.
My guess is that in general the nested loops (together with suitable preallocation) would be the fastest approach, but rather than rely on my guesswork you could just test this yourself:

Sign in to comment.

Matt J
Matt J about 1 hour ago
Edited: Matt J about 1 hour ago
Do not use a nested loop:
clear Data
for k=250:-1:1
Data(:,k,:) = Matrix(zapTime(k)-12000:zapTime(k)+75000, 2:end) );
end
subData=reshape( num2cell(Data,[1,2]) ,1,[]) ;

Asked:

on 4 Mar 2026 at 18:02

Edited:

about 15 hours ago

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!