structures, cells or high dimensional arrays

Dear all,
I have to deal with arrays of dimension 4 or 5. So far, I can only think of 3 ways to represent such arrays.
  1. Using matrices only, the representation of the i-j-k-l-m can be written as A(i,j,k,l,m)
  2. using structures, it would be A(i).V(j,k,l,m)
  3. finally using cells, it is A{i}(j,k,l,m)
My problem is that I have non-vectorizable loops around those elements and I was wondering 1) which one of those representations has the greatest speed, 2) whether there are more efficient representations and 3) whether it is possible to accelerate operations involving such arrays.
Thanks, Patrick

 Réponse acceptée

Matt J
Matt J le 24 Juin 2013
Modifié(e) : Matt J le 24 Juin 2013

0 votes

Why is calling say P faster than calling P(:,:)?
The indices (:,:) have to be processed in the second case, just like any other indexing expression, e.g., P(:,1:2:end).
Perhaps the MATLAB developers could optimize things by giving special treatment to the colons-only syntax (:,:,...,:) but why would they bother? Who would ever use P(:,:) when just P is enough?

5 commentaires

Suppose A is 2 dimensional and we have that
P(:,:,j)=A ,Q{j}=A and R(j).V=A.
My struggle is about finding out which one, Q, P or R will be processed the fastest.
Matt J
Matt J le 24 Juin 2013
Modifié(e) : Matt J le 24 Juin 2013
But you already said "I plan to formally test the three versions."
You need to make up your mind which of the 2 questions you've posed in this thread you want to focus on. Ideally, your question abotu P vs. P(:,:) would be moved to another thread.
Patrick Mboma
Patrick Mboma le 24 Juin 2013
Sorry Matt, I thought they were directly related.
James Tursa
James Tursa le 24 Juin 2013
Modifié(e) : James Tursa le 25 Juin 2013
@Patrick: FYI,
P(:,:,j)=A % This copies the A data into P
Q{j}=A % This makes a shared data copy of A and puts the address of it into Q
R(j).V=A % This makes a shared data copy of A and puts the address of it into R
Subsequent operations that you do will typically drive which one is fastest for your application. E.g., doing P(:,:,j) downstream will cause a data copy of the entire contents of P(:,:,j) to be made if it appears on the right-hand-side of an assignment or as an argument to a function, whereas doing Q{j} downstream will only cause a shared data copy of Q{j} to be made. So for read-only purposes you may find that Q{j} is faster. But if you are only modifying the contents downstream and P(:,:,j) only appears on the left-hand-side of assignments, then P(:,:,j) may end up being faster for you. The "fastest" question can only be answered by your testing and how you are accessing the data downstream.
Patrick Mboma
Patrick Mboma le 25 Juin 2013
@ James, Thank you for these clarifications

Connectez-vous pour commenter.

Plus de réponses (1)

Patrick Mboma
Patrick Mboma le 24 Juin 2013

0 votes

I plan to formally test the three versions. But a more simple question is the following: suppose we just have 2 dimensions. Why is calling say P faster than calling P(:,:)?

1 commentaire

Sean de Wolski
Sean de Wolski le 24 Juin 2013
Modifié(e) : Sean de Wolski le 24 Juin 2013
passing P, passes a reference to P and does not make a memory copy. Indexing into P using P(:,:) copies the memory into a new array. You can see this by:
Opening the task manager (Windows) and looking at performance -> physical memory usage.
Then run:
A = magic(10000);
You'll see a jump in memory. If you then run:
B = A;
No change, since B is merely a reference to A.
C = A(:,:)
It jumps again.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by