A single column vector or an array, which is faster?

6 ビュー (過去 30 日間)
Zheng Zhao
Zheng Zhao 2020 年 2 月 11 日
編集済み: James Tursa 2020 年 2 月 11 日
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
  1. indexing. e.g., A(i*j) vs B(i,j)
  2. Assignment e.g., A(i*j) = x vs B(i,j)=x
  3. Memory efficiency. Do they allocate the same memory size?
What would be the pros and cons choosing a vector or array for storing data?

採用された回答

James Tursa
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 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by