Indexing n cell elements at a time

1 次查看(过去 30 天)
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).

采纳的回答

James Tursa
James Tursa 2015-7-7
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 个评论
R J
R J 2015-7-7
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by