Indexing n cell elements at a time

조회 수: 1 (최근 30일)
R J
R J 2015년 7월 7일
댓글: R J 2015년 7월 7일
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