Is slicing one big matrix more efficient than storing smaller matrices in a struct?

10 views (last 30 days)
An example of the former would be:
% allcases =
%
% +---------+------------+------------+------------+-----+-------------+
% | Case ID | Data col 1 | Data col 2 | Data col 3 | ... | Data col 10 |
% +---------+------------+------------+------------+-----+-------------+
% | 1 | scalar | scalar | scalar | ... | scalar |
% +---------+------------+------------+------------+-----+-------------+
% | 1 | scalar | scalar | scalar | ... | scalar |
% +---------+------------+------------+------------+-----+-------------+
% | 1 | scalar | scalar | scalar | ... | scalar |
% +---------+------------+------------+------------+-----+-------------+
% | ... | ... | ... | ... | ... | ... |
% +---------+------------+------------+------------+-----+-------------+
% | n | scalar | scalar | scalar | ... | scalar |
% +---------+------------+------------+------------+-----+-------------+
An example of the latter would be:
% allcases(case_id).matrix =
%
% +------------+------------+------------+-----+-------------+
% | Data col 1 | Data col 2 | Data col 3 | ... | Data col 10 |
% +------------+------------+------------+-----+-------------+
% | scalar | scalar | scalar | ... | scalar |
% +------------+------------+------------+-----+-------------+
% | scalar | scalar | scalar | ... | scalar |
% +------------+------------+------------+-----+-------------+
% | scalar | scalar | scalar | ... | scalar |
% +------------+------------+------------+-----+-------------+
% | ... | ... | ... | ... | ... |
% +------------+------------+------------+-----+-------------+
% | scalar | scalar | scalar | ... | scalar |
% +------------+------------+------------+-----+-------------+
Now, in a loop that goes through the cases, it needs to work with a (smaller) matrix that concerns that case only. Therefore, with the former approach we would slice the matrix (via logical or a find) while in the latter we would just call allcases(case_id).matrix.
Is there a recommended approach between these two, in terms of computational speed?
In my problem, I work with up to 100 cases, and up to 50,000 rows in the matrix containing data for each case.
  3 Comments
Sindar
Sindar on 25 Oct 2020
If the data/case ID is constant, and depending on what you're doing with the data, it may be useful to use paged arrays, so
allcases(:,:,case_id)
returns the same matrix as your
allcases(case_id).matrix
For example, 2020b introduced pagewise matrix multiplication which is almost certainly faster than looping over the index subsets (or a structure array)

Sign in to comment.

Accepted Answer

Matt J
Matt J on 6 Nov 2020
Edited: Matt J on 6 Nov 2020
Therefore, with the former approach we would slice the matrix (via logical or a find) while in the latter we would just call allcases(case_id).matrix..
Either one can be more efficient, depending on the situation. Slicing the matrix requires a memory allocation operation. On the other hand, if you have a very large number of matrix IDs, I think you will find that the non-contiguous storage in RAM that you will have with a struct will start to outweigh what you save by avoiding matrix slicing.
Incidentally, you might also consider using a cell array,
allcases{case_id}=matrix
which also avoids matrix slicing, but which should give you slightly faster indexing than a struct.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by