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

6 views (last 30 days)
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?

Accepted Answer

James Tursa
James Tursa on 11 Feb 2020
Edited: James Tursa on 11 Feb 2020
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.

More Answers (0)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!