MATLAB Answers

Adding elements to the ends of vectors inside a cell array of vectors

1 view (last 30 days)
Is there a way to add elements to the ends of vectors that are inside a cell array of vectors which would be faster than the following code that uses a for-loop?
x = rand(1,20);
i1 = [1,7,11];
i2 = [6,10,20];
xLower = [0.1 0.2 0.3];
xUpper = [100 200 300];
tic();
for r = 1:length(i1)
xRegion{r} = [xLower(r) x(i1(r):i2(r)) xUpper(r)];
end
toc();
xRegion

Accepted Answer

Eike Blechschmidt
Eike Blechschmidt on 29 Jul 2021
You could use arrayfun:
arrayfun(@(l,i1,i2,u) [l x(i1:i2) u], xLower,i1,i2, xUpper, uniform, false)
This is untested but should be faster.
  8 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!