Preallocation of data
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
2011년 4월 26일
1 개 추천
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
2011년 4월 26일
Yes, but I m still blur on it.
C=[];
for i=1+5000*looptimes: last+5000*looptimes
C=[C F((-15+16*(i)):16*(i))];
end
Matt Fig
2011년 4월 26일
And what are the values for looptimes and last?
MrPuzzled Marc
2011년 4월 26일
The answer you gave to me is to fill each column but now i want to fill it horizontally. Something like zeros (1,M)....
MrPuzzled Marc
2011년 4월 26일
Is the size of the file. Last is the remainder after division if the file size cannot be fully divisible by 5000.
[M N]=size(A);
looptimes=fix(M/5000);
last=rem(M, 5000);
MrPuzzled Marc
2011년 4월 26일
Ok, i finally got what you mean. Thanks. Anyway, preallocation could not be assigned when there is no for loop? Like for example my code earlier on?
received=fgetl(s);
file= [file received];
The received is the received bits from the serial port and then 16bits is stored back onto file. I cannot preallocate it?
MrPuzzled Marc
2011년 4월 26일
Is this FILE still can be preallocate it for certain values so that when it keep receiving it the data will save the execution time?
MrPuzzled Marc
2011년 4월 27일
How to preallocate for this one?
[M N]=size(A);
looptimes=fix(M/5000);
last=rem(M,5000);
send_count=1;
C=[];
for i=1+5000*(send_count-1):5000*(send_count)
C=[C F((-15+16*(i)):16*(i))];
end
send_count=1+send_count;
??????
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
2011년 4월 27일
Hi mr matt, I think replace (M-last) with (5000*looptimes) will yield a better results. Thanks for your help.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
참고 항목
2011년 4월 26일
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
