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
  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 CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by