Is slicing one big matrix more efficient than storing smaller matrices in a struct?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
dpb
el 25 de Oct. de 2020
The struct will create a fair amount of storage overhead plus have to create it.
Is the number of observations per case fixed and the same? If that were so, then straight indexing will return the values by case without the overhead of the logical matching altho since don't need the numeric indices, dispensing with the unneeded find() will be somewhat faster.
Overall, 50,000/100 ==> ~500 rows/case is pretty trivial case size; unless there are some iterative calculations or other very compute-intensive analyses, I doubt it'll make much difference at all. But, I'd put my money on not using struct as the winner simply owing to the higher-level abstraction thereof.
Sindar
el 25 de Oct. de 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)
Respuesta aceptada
Matt J
el 6 de Nov. de 2020
Editada: Matt J
el 6 de Nov. de 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.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!