フィルターのクリア

A fast cell array generation

5 ビュー (過去 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
tic
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?)
v_indices{i}=v(indices{i});
end
end
toc
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 件のコメント
Jan Valdman
Jan Valdman 2020 年 11 月 5 日
Dear Bruno, thank you for your tips, I am thinking about them.
In 2D, we end up with a local vector with has a variable size (depending on number of elements a node is shared by), so it is for instance someting between 3 and 'na', where 'na' is the number of adjacent nodes, for simplicity let us take na=20 for a nice geometry. In 1D, it is, as you are suggesting, bounded by 3 (it is in fact 2 for boundary nodes and 3 for internal nodes). So we have to figure out how to address and process these local contributions efficiently. I am thinking of precomputing the value 'na' (it follows from the mesh topology) and perhaps putting zero value at positions not used. Let us see.
Bruno Luong
Bruno Luong 2020 年 11 月 5 日
編集済み: 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.

サインインしてコメントする。

回答 (1 件)

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 日
編集済み: 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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeParallel Computing Fundamentals についてさらに検索

タグ

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by