A single column vector or an array, which is faster?
6 ビュー (過去 30 日間)
古いコメントを表示
Let's say we have a column vector A with size (N*M, 1) and an array B with size (N, M). N and M are very large numbers.
Which of the strucute has faster speed in terms of
- indexing. e.g., A(i*j) vs B(i,j)
- Assignment e.g., A(i*j) = x vs B(i,j)=x
- Memory efficiency. Do they allocate the same memory size?
What would be the pros and cons choosing a vector or array for storing data?
0 件のコメント
採用された回答
James Tursa
2020 年 2 月 11 日
編集済み: James Tursa
2020 年 2 月 11 日
Other things you are doing in your code are likely to dominate run times. We would need to see your particular application to offer better advice.
Linear indexing for calculations and assignment might be slightly faster in the background, but I would be surprised if you could even tell the difference with any testing you could do.
Memory size is the same.
The pros & cons of one vs the other are entirely dependent on how you would be accessing and using the data downstream in your code. Note that for a full matrix M, the espressions M(:) and reshape(M,N*M,1) both result in shared data copies (i.e., no deep data copy) and are extremely fast. So you can work with both simultaneously downstream in your code depending on how you want to access the data. And, even if M is a 2D matrix, you can still use linear indexing on it without even reshaping it first.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Multidimensional Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!