필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Ankitkumar Patel
Ankitkumar Patel 2022년 5월 31일
편집: Jan 2022년 6월 1일
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