Indexing n cell elements at a time

1 vue (au cours des 30 derniers jours)
R J
R J le 7 Juil 2015
Commenté : R J le 7 Juil 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).

Réponse acceptée

James Tursa
James Tursa le 7 Juil 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 commentaire
R J
R J le 7 Juil 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by