concat/horzcat for cell with data cells of a*b*c sizes
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Jan
le 31 Mai 2022
E1m = reshape(E1, 180, []);
E2m = reshape(E2, 180, []);
E = cat(2, E1m, E2m);
cat(2, ...) is the same as horzcat().
2 commentaires
Ankitkumar Patel
le 31 Mai 2022
Modifié(e) : Ankitkumar Patel
le 31 Mai 2022
Jan
le 1 Juin 2022
Modifié(e) : Jan
le 1 Juin 2022
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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Entering Commands dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!