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

조회 수: 8 (최근 30일)
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
dpb
dpb 2020년 10월 25일
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
Sindar 2020년 10월 25일
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)

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2020년 11월 6일
편집: Matt J 2020년 11월 6일
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개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by