Indexing n cell elements at a time

1 view (last 30 days)
R J
R J on 7 Jul 2015
Commented: R J on 7 Jul 2015
Hi all,
I have a 1x100 cell (comprised of 2x500 doubles) and am trying to index through it 4 cells at a time. What I mean is that I would like to index cells 1-4, 9-12, then 17-20 and so on. Im having a bit of a hard time with this and would appreciate and comments or suggestions. Thank you!
An example:
for j = 1:4:100 % Index through 1x100 cell
for k = 1:4
temp{k}(1,:) = values{k}(1,:);
end
end
I will be editing this as I work on it! Thanks again!
EDIT: I found an answer to a similar question that proposed this:
idx = setdiff(2:10000,5:4:10000);
for ii = idx
disp(ii)
end
Again, the issue I run to here is that I can specify to skip every nth element but not a group of elements (i.e. 4 elements at a time).

Accepted Answer

James Tursa
James Tursa on 7 Jul 2015
E.g., is this the type of indexing you want?
for j = 1:4:100 % Index through 1x100 cell
temp = values(j:j+3); % Pick off the 4 cells
for k = 1:4
% Use temp{k} here
end
end
  1 Comment
R J
R J on 7 Jul 2015
Thank you! This was very helpful and works. Another solution I came across was this (thanks to the cyclist):
idx = bsxfun(@plus,[0 1 2 3]',1:8:100)
for ii = idx(:)
% Do stuff
end

Sign in to comment.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing 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!