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!