Which is more efficient: iteratively filling in a sparse matrix vs. creating a new sparse matrix every time i need to update the matrix?

7 ビュー (過去 30 日間)
I am running a time series model where at each time step I convert some zero terms in a sparse matrix to non-zero terms. Here is a simple example:
At time point 1, I start off with non-zero terms in row 1 only:
At time point 2, I may (or may not) add a non-zero term to row 2 of any of the columns:
At time point 3, I may (or may not) add a non-zero term to row 3 of column 1, row 2 of column 2, or row 2 of column 3:
In general, at each time point, I may (or may not) add a non-zero term to the next available row that contains a zero. Thus, at time point n, each column will have non-zero terms extending between row 1 and row n (inclusive). The actual sparse matrices I will be working with (and multiplyng together) will be of the size 10 x 1000. Note I will be doing lots of operations of this kind at each point in time, hence concerns regarding effciency.
Because of the sequential nature of the time series model, I cannot fill the entire matrix in from time point 1. My question is:
  1. Will it be more efficient (faster) to create a sparse array from the start with space allocated for future non-zero terms, and then fill in the non-zero terms at each time step (as above), or will it be faster to create a new sparse matrix at each time step with the appropriate non-zero terms specified and delete the previous sparse matrix?
Many thanks in advance
  4 件のコメント
Stephen23
Stephen23 2019 年 3 月 15 日
"The actual sparse matrices I will be working with (and multiplyng together) will be of the size 10 x 1000. Note I will be doing lots of operations of this kind at each point in time, hence concerns regarding effciency."
Storing and processing 10x1000 matrices as sparse matrices is unlikely to be efficient anyway. If efficiency is your concern, ditch the sparse matrices.
James Heald
James Heald 2019 年 3 月 15 日
Just to clarify, are you claiming that the use of sparse matrices will a) provide little benefit or b) worsen performance?

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

回答 (1 件)

James Tursa
James Tursa 2019 年 3 月 15 日
Iteratively changing a sparse matrix causes the entire data set to be copied each time, so this is inherently not efficient and will slow you down no matter which way you do it. One method might be faster than another for your particular case and probably the only way to know is to try it out for your particular sizes as Adam suggests. But for a 10x1000 size, I would also consider just using a full matrix and see how that timing compares.
  4 件のコメント
Matt J
Matt J 2019 年 3 月 15 日
Basically, the sparse data is stored in a sorted fashion, so inserting data effectively causes a re-sort and data will need to be copied to accomodate the insertion.
However, since the data is being added row-wise, perhaps this can be avoided by transposing the matrix and adding elements column-wise instead. I wonder if this might then avoid a re-sort.
James Tursa
James Tursa 2019 年 3 月 16 日
編集済み: James Tursa 2019 年 3 月 16 日
Yes, this could in theory work as you state. Since the result of the re-sort is to always append new data to the "end" of the current data, none of the current data would need to be copied. (OP would need algorithms that worked with the transpose, of course)

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

カテゴリ

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