A fast cell array generation
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Bruno Luong
am 5 Nov. 2020
Bearbeitet: Bruno Luong
am 5 Nov. 2020
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.
Antworten (1)
Walter Roberson
am 5 Nov. 2020
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 Kommentar
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!