concat/horzcat for cell with data cells of a*b*c sizes

1 次查看(过去 30 天)
Read variables from files
ncvars={'a','b'}; % variables need from files ncvar{1}=a, ncvars{2}=b
prjdir=[datadir 'mentioned path']; % path to directory
dinfo=dir(fullfile(prjdir,'*.nc')); %taking all files with extension .nc
num_files=length(dinfo); % number of files
E1=cell(num_files,1); % defining cell
E2=cell(num_files,1);
% taking a and b from files and putting in E1 and E2 respectively
for K = 1:num_files
file=filenames{K};
E1{K}=ncread(file, ncvars{1});
E2{K}=ncread(file,ncvars{2});
end
Now the question is I want to join/concat/horzcat E1 and E2.
Where I have E1 and E2 of 2*1 cell. (2 is number of files)
When I had E1{1}=180*161 double and E1{2}= 180*146. I used folowing to horzcat:
Etemp=horzcat(E1{:},E2{:}); %Etemp is 180*614 where 614 because 2*(161+146)
E=Etemp(1:end) % E dimention 1*110520 double
Now I have E1{1}=180*161*253 double and E1{2}=180*146*127 double. Same for E2 too.
How can I use horzcat for E1 and E2 in this case??
So that I can have Etemp dimention someting like 180*100008 where because 2*(161*253+146*127)???

采纳的回答

Jan
Jan 2022-5-31
E1m = reshape(E1, 180, []);
E2m = reshape(E2, 180, []);
E = cat(2, E1m, E2m);
cat(2, ...) is the same as horzcat().
  2 个评论
Ankitkumar Patel
Ankitkumar Patel 2022-5-31
编辑:Ankitkumar Patel 2022-5-31
It is usefull some what. These gave divisible error.
Here E1 and E2 are 2*1 cell. So with some changes in these lines I able to create for E1 and E2 separately
E1m=reshape(E1{1},180,[]); % 180*40733
E11m=reshape(E1{2},180,[]); %180*18542
E1 =cat(2,E1m,E11m); %180*59275
Same way will get E2 and then after cat for E1 and E2 final result will be achived.
Is there any way where we can create loop for these??
Jan
Jan 2022-6-1
编辑:Jan 2022-6-1
Yes.
C = cell(1, 2);
for k = 1:2
C{k} = reshape(E1{k}, 180, []);
end
E1M = cat(2, C{:});
A compact (but not faster) solution:
C = cellfun(@(x) reshape(x, 180, []), E1, 'UniformOutput', 0);
E1M = cat(2, C{:});
I use a new name instead of re-using E1. Letting a variable change its type from cell to double impedes the JIT acceleration. It is not a bug, but faster to avoid this.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by