Buffering in a matrix the most time efficient way

16 vues (au cours des 30 derniers jours)
Pascal Weller
Pascal Weller le 18 Nov 2020
Hello everyone,
I am currently poking a little bit in the dark while trying to find an answer to the question formulated in this post's title.
Here are the boundary conditions for my specific case:
  1. I want to buffer data in a matrix A of size m x n, where m always stays at a fixed value and n fluctuates between zero and a maximum Nmax.
  2. Data that has to be buffered is of size m x k (matrix B).
  3. Whenever data in B is available, it has to be buffered (A = [A,B]) until A hits is maximum length n == Nmax. (In case not all k columns of B still fit into A the remaining columns have to be stored and buffered into A once there is space available again.)
  4. Once A is full further computations on its content are conducted.
  5. After these computations terminate a part of A (e.g. the first half) has to be discarded (A = A(:,round(size(A,2)/2):end)).
  6. Now the program loops back to 3. and starts all over again.
%% Dummy code
while(true)
B = GetData(); % of size m x k
while ~isempty(B)
% check whether all k columns of B fit into A
% if yes buffer all of them into A, if not
% buffer the ones that fit and keep the remaining
% ones for the next iteration of while ~isempty(B)
...
if size(A,2) == Nmax
... % Do computations
A = A(:,round(size(A,2)/2):end); % discard data that is not needed anymore (e.g. first half of A's columns)
end
end
end
Option 1: Now I could just do it the way I depicted in the dummy code but there is a lot of memory allocation and freeing up going on. As from what I read in other posts here on the MATLAB Answers forum the allocation procedure can be very time consuming. Thats why I would love to preallocate the memory needed by matrix A at is maximum size (m x Nmax) and always keep that amount of memory allocated for A even if it changes in size (see step 5.). Is there a way to do so? (Kind of like the C malloc() function).
Option 2: I also thought about initializing A with A = zeros(m,Nmax). In step 5. one could copy A's remaining second half to its first half and fill the second half with zeros again. Somehow like in the code below:
A(:,1:round(size(A,2)/2)) = A(:,round(size(A,2)/2):end);
A(:,round(size(A,2)/2)+1:end) = 0;
This solution however imposes the dificulty of determining A's current size that is actually filled with real data. In the current case size(A,2) will always return Nmax. Remember that the portion of A still being filled with real data as well as size(B,2) vary from iteration to iteration. To correctly buffer B's data into A one always has to search for A's first zero-column to then continue buffering from this column on. Is there a more efficient way to search for the first zero column than shown in the following code snippet?
for column = 1:size(A,2)
if ~any(A(:,column))
column = buffStartIdx;
end
end
A(:,buffStartIdx:buffStartIdx+size(B,2)) = B;
Would this current option be faster than option 1 (considering all the searching and writing A's old columns to zero)?
Option 3: The last option that I thought of is to dedicate a persistent variable (say currIdx) to the task of tracking A's last column that is currently filled with real data. In the inital run A can still be initialized with A = zeros(m,Nmax). Whenever B is buffered into A currIdx will be updated accordingly. This way A will not change in size over time, no searching for the first zero-colum is necessary and filling A's obsolete columns back with zeros can be omitted. Despite the advantages in computation time I see one potential disadvantage: currIdx's integrity. Option one and two both base their evaluation on where to start buffering on the actual buffering matrix A itself (in every single iteration). In contrast option three's evaluation is based on a variable that in itself is independent of A. Once currIdx gets messed up (for god knows what reason) the whole buffering process is invalid. What are your thouhts? Is that a thing to be worried about?
Thanks to everyone who made it this far into the post.
I am thankful for any kind of input or potential solution(s) to my problem!

Réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by