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 日
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 で Creating and Concatenating Matrices についてさらに検索
参考
2011 年 4 月 26 日
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
