How to take first element from the cell?
조회 수: 2 (최근 30일)
이전 댓글 표시
Dear all,
I have a cell
d={[3,80,32,13] [3,80,17] [3,45,18] [3,45,20] [3,80,32] [3,45] [3,80,47] [3,45,93,62] [3,80] [3,45,93]}
Also I have vector c= [80 45];
The question is : How to take the first element that locate it after vector c numbers ?
The expected result is =
{[32] [17] [18] [20] [32] [] [47] [93] [] [93]}
Also I would like to get other elements that locate it after the first element in another cell, for example:
x = {[13] [] [] [] [] [] [62] [] []}
please help>>>>
댓글 수: 0
답변 (1개)
Stephen23
2018년 7월 29일
편집: Stephen23
2018년 7월 29일
Something like this (untested, but should get you started):
N = numel(d);
R = cell(1,N);
X = cell(1,N);
for k = 1:N
idx = ismember(d{k},[80,45]);
idx = [false,idx(1:end-1)];
if any(idx)
R{k} = d{k}(find(idx,1,'first'));
end
idx = [false,idx(1:end-1)];
if any(idx)
X{k} = d{k}(find(idx,1,'first'));
end
end
댓글 수: 2
Stephen23
2018년 7월 30일
@laith Farhan: I hope that it helped. Remember to accept the answer that helped you most: accepting answers is the easiest way to show your appreciation to the volunteers who help you on this forum.
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!