フィルターのクリア

Fast FIFO Array/Other Datatype

32 ビュー (過去 30 日間)
Matthew Mellor
Matthew Mellor 2016 年 6 月 14 日
編集済み: Andrew Ward 2018 年 11 月 27 日
I'm trying to plot something in real time using an FIFO array. I'm implementing the array using the advice someone else here gave me (thank you!) of the following sort:
array = {1,2,3,4};
array = array(2:end)
array{end+1} = 5
Unfortunately this is a bit too slow for what I'm trying to do :(. Could anyone suggest either an efficient FIFO array-like data structure? Or just a faster way?
  2 件のコメント
Star Strider
Star Strider 2016 年 6 月 14 日
Adam
Adam 2016 年 6 月 14 日
You could probably also use a containers.Map though I don't know what its speed would be like compared to your approach or Walter's answer which is probably faster since it uses a raw array without memory copying.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2016 年 6 月 14 日
array(1:end-1) = array(2:end);
array{end} = newvalue;
This avoids having to grow the array.
  2 件のコメント
Matthew Mellor
Matthew Mellor 2016 年 6 月 15 日
Thanks so much!
Kunle Olutomilayo
Kunle Olutomilayo 2017 年 6 月 24 日
Awesome.

サインインしてコメントする。

その他の回答 (1 件)

Andrew  Ward
Andrew Ward 2018 年 11 月 27 日
編集済み: Andrew Ward 2018 年 11 月 27 日
Nmax=50000;
A1=1:Nmax;
Ap=A1;
%%
%Method 1 concatenate array with element - Slow
tic
for i=1:Nmax
A1=[A1(2:end),Nmax+i];
end
toc
%%
%% Method 2 overwite ellemnts followed by a circular shift - Fastest -
% avoids taking a sub portion of the array
% which I think is costly, just replaces the value, 1,2,3,4
% becomes 5,2,3,4, so to get it in order you need to
% do the circular shift at the end.
tic
A2=1:Nmax;
for i=1:Nmax
ic=mod(i,Nmax+1);
A2(ic)=Nmax+i;
end
A2=circshift(A2,-i);
toc
sum(A2~=A1)
%% Method from above, still slow
A3=Ap;
tic
for i=1:Nmax
A3(1:end-1)=A3(2:end);
A3(end)=Nmax+i;
end
toc
sum(A3~=A1);

カテゴリ

Help Center および File ExchangeSparse Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by