Can anyone help me figure out the preallocation of the unknown matrix which no. of rows changing in every cycle? Thank in advance

조회 수: 1 (최근 30일)
close all; clc;
tic
p=dir('*.txt');
nFiles=numel(p);
A_cat=[]; % A_cat=zeros(500000,30);
for k=1:nFiles
A=dlmread(p(k).name);
disp(size(A_cat)); %% you can check the no. of rows is increasing
%A_cat=[A_cat;A]; %% every cycle, matrix changes its size!!!
A_cat=vertcat(A_cat,A);
end
toc

채택된 답변

KSSV
KSSV 2021년 2월 23일
편집: KSSV 2021년 2월 23일
close all; clc;
tic
p=dir('*.txt');
nFiles=numel(p);
A_cat=cell(nFiles,1); % A_cat=zeros(500000,30);
for k=1:nFiles
A=dlmread(p(k).name);
disp(size(A_cat)); %% you can check the no. of rows is increasing
%A_cat=[A_cat;A]; %% every cycle, matrix changes its size!!!
A_cat{k} = A;
end
A = vertcat(A_cat{:}) ;
toc
  댓글 수: 5
KSSV
KSSV 2021년 2월 23일
Try other functions instead of dlmread. Have a look on load, importdata.
SA
SA 2021년 2월 23일
do you think the other possibility to run the code faster?
tic;
P_avg=cell(1,30);
for j =1:30
P_dat = buffer(A(:,j),500); %% cut the 500 elements*1000 of a single sensor
P_m=mean(P_dat,2);
P_avg{j}=P_m;
end
mn_dat = horzcat(P_avg{:}) ;
toc;

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by