Indexing n cell elements at a time

1 Ansicht (letzte 30 Tage)
R J
R J am 7 Jul. 2015
Kommentiert: R J am 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).

Akzeptierte Antwort

James Tursa
James Tursa am 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 Kommentar
R J
R J am 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by