A fast cell array generation

조회 수: 2(최근 30일)
Jan Valdman
Jan Valdman 2020년 11월 5일
편집: Bruno Luong 2020년 11월 5일
Dear all,
in our optimization procedure, we need to create a cell array 'v_indices' from a vector 'v' and another cell array of indices 'indices'. Here is my code:
repets=4000; %repetitions
n=1000; %vector length
%defines a cell of indices
indices = mat2cell([0:n-1; 1:n; 2:n+1]',ones(n,1),3);
indices{1}=[1 2]; %correction of the first entry
indices{n}=[n-1 n]; %correction of the last entry
for r=1:repets
%defines a random vector
v = randi(n*10,n,1);
v_indices=indices; %preallocation
for i=1:numel(indices) %loop over cell (inefficient?)
The parameter 'repets' can be set higher in tests. Perhaps a for loop generating 'v_indices' is not very efficient there. Can you suggest some improvement, if possible?
Thank you, Jan Valdman
  댓글 수: 6
Bruno Luong
Bruno Luong 2020년 11월 5일
And when you connect all the adjadcent nodes do you get a constant-vertex polygonals such as triangles, rectangles, pentagonals, heaxagonals ? If it the case the best is vertexe/face number then you can "loop" on face to compute the local enegy operator.
If not there is another structure more generic is graph/digraph. Not sure about the speed though.
Under the hood of graph there is a sparse adjacent matrix. It can be of class logical if you are only interested in adjacency relation ship and you can work with such data structure as well. For example you can imgaine you build a sparse matrix such that when you multiply by the potential it returns the gradient field. Then you can compute the local p-norm, etc...
Bottom line is use two/three bigs arrays to store the topology. Avoid cell.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2020년 11월 5일
You hae v_indices=indices inside your for r loop, so nothing assigned to v_indices is kept for the next iteration of r. You do not assign to indices within the loop, so running multiple times is not making iterative changes to indices.
Therefore, your final result in v_indices is going to be the same as if you had only done one (the last) iteration of the for r loop, so you might as well not have a for r loop there.
  댓글 수: 1
Jan Valdman
Jan Valdman 2020년 11월 5일
Thank you, you are right: the cell array 'v_indices' is recomputed inside the loop. The code is only a simplified benchmark, there is an extra operation missing on 'v_indices' and needed - it is an energy computation expained in my answer to Bruno above.

댓글을 달려면 로그인하십시오.





Community Treasure Hunt

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

Start Hunting!

Translated by