2 views (last 30 days)

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

Walter Roberson
on 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.

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

Start Hunting!
## 6 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/636960-a-fast-cell-array-generation#comment_1111450

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/636960-a-fast-cell-array-generation#comment_1111450

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/636960-a-fast-cell-array-generation#comment_1111670

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/636960-a-fast-cell-array-generation#comment_1111670

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/636960-a-fast-cell-array-generation#comment_1111760

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/636960-a-fast-cell-array-generation#comment_1111760

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/636960-a-fast-cell-array-generation#comment_1111790

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/636960-a-fast-cell-array-generation#comment_1111790

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/636960-a-fast-cell-array-generation#comment_1111915

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/636960-a-fast-cell-array-generation#comment_1111915

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/636960-a-fast-cell-array-generation#comment_1111945

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/636960-a-fast-cell-array-generation#comment_1111945

Sign in to comment.