Preallocation of data

조회 수: 1 (최근 30일)
MrPuzzled Marc
MrPuzzled Marc 2011년 4월 26일
Hi, I have a code which stated it suggest to preallocate to consider speed. I have a incoming data and I stored as received. Here is the code:
received=fgetl(s);
file= [file received];
How can I preallocate the file when I m receiving? The formation of data should look like file =01010101010101010101010101010000.........
How can i do that?
Thanks

채택된 답변

Matt Fig
Matt Fig 2011년 4월 26일
Is this just the same question you have asked twice before? If not, you need to include more code. For example, pre-allocation works when used in a loop, yet I see no loop in what you have posted above. Post the code of the loop where you are doing the concatenation.
%
%
%
EDIT In response to your comments below...
Yes, I built the array by column, but then reshaped it to a row vector. You get what you want, and this is faster than some complex indexing scheme into C. Just try it before you think it won't end up a row vector!
Idx = 1+5000*looptimes:last+5000*looptimes;
C = zeros(16,last); % Start out this way, fix in last line...
for ii = 1:length(Idx)
C(:,ii) = F((-15+16*(Idx(ii))):16*(Idx(ii)));
end
C = reshape(C,1,16*last); % Make into a row vector!!
  댓글 수: 9
MrPuzzled Marc
MrPuzzled Marc 2011년 4월 27일
Hi Mr Matt, I have written this to replace it. However, I m not sure if the minus will minus the data correctly. For example, I have a file size of 6353. My data is divided to send 5000, and 1353(Remainder). However, if i wrote in this manner will it gives the 5000 that i wanted?
Idk=(1+5000*(send_count-1)):(5000*send_count);
C=zeros(16,(M-last));
for i=1:length(Idk)
C(:,i)=F((-15+16*(Idk(i))):16*(Idk(i)));
end
C=char(C);
C=reshape(C,1,16*(M-last));
send_count=1+send_count;
Is the (M-last) will gives the correct front 5000bits that I wanted?
MrPuzzled Marc
MrPuzzled Marc 2011년 4월 27일
Hi mr matt, I think replace (M-last) with (5000*looptimes) will yield a better results. Thanks for your help.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by