A fast cell array generation
Show older comments
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 Comments
Bruno Luong
on 5 Nov 2020
CELL and INDEXING is inherently slow (not the for-loop which is fast). You won't have a faster code structured like this.
If you explain what you would do with v_indices mighte be there is a fast way to carry out the calculation without breaking up sliding 3-window in cell (such as conv, im2col, blockproc, etc...).
Jan Valdman
on 5 Nov 2020
Edited: Jan Valdman
on 5 Nov 2020
Bruno Luong
on 5 Nov 2020
Edited: Bruno Luong
on 5 Nov 2020
I'll leave out the detail of plus/minus potential and let you sort out, but instead of cell you can form an n x 3 array and work on this array. (NOTE: Your original code looks odd to me if not buggy).
The idea is this code:
repets=2000; %repetitions
n=1000; %vector length
p=2;
h=1/numel(n); % to me it's not correct
for r=1:repets
v = randi(n*10,n,1); %defines a random vector
vpad = v([1 1:end end]);
v_loc = [vpad(1:end-2) vpad(2:end-1) vpad(3:end)];
Dv_local = diff(v_loc,1,2) / h;
e_local = (1/p)*h*sum(Dv_local.^p,2); % h would cancel out, p norm require power of (1/p), not devided by p
end
Bruno Luong
on 5 Nov 2020
Edited: Bruno Luong
on 5 Nov 2020
"we would like to keep the cell array for an extension to 2D, where it can not be avoided at all."
Usually the right data structure for (mesh) connectivity topology if double array vertex/face. One does not need CELL.
Avoid calculation with CELL when you can if speed matter for you.
Jan Valdman
on 5 Nov 2020
Bruno Luong
on 5 Nov 2020
Edited: Bruno Luong
on 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.
Answers (1)
Walter Roberson
on 5 Nov 2020
0 votes
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 Comment
Jan Valdman
on 5 Nov 2020
Edited: Jan Valdman
on 5 Nov 2020
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!