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)???
0 个评论
采纳的回答
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 个评论
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 Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!