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!

답변 (2개)

Stephen23
Stephen23 대략 4시간 전
편집: Stephen23 대략 3시간 전
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

Garyn
Garyn 대략 3시간 전
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.
Stephen23
Stephen23 대략 2시간 전
편집: Stephen23 대략 2시간 전
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:

댓글을 달려면 로그인하십시오.

Matt J
Matt J 대략 2시간 전
편집: Matt J 대략 1시간 전
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,[]) ;

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2026년 3월 4일 18:02

편집:

대략 13시간 전

Community Treasure Hunt

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

Start Hunting!

Translated by