Remove all number that comes after in cell array

조회 수: 5 (최근 30일)
Jonathan Cheong
Jonathan Cheong 2021년 3월 8일
댓글: Jonathan Cheong 2021년 3월 10일
Hello again, I have a 45x1 cell 'ddindexcell' with numbers of different dimensions. Then I have a list of numbers 'indend2'.
What I'd like to achieve is to remove all the numbers that comes after indend2, if found in the cell.
Example:
A = [1, 2, 3, 4, 5] B = [3, 59, ... ]
[20]
[57, 58, 59, 60]...
The output would be:
Output = [1, 2, 3]
[20]
[57, 58, 59]
Perhaps changing it into an array would make the whole process friendlier, but I'm not sure.
How can I acheive this? Thanks in advance.
  댓글 수: 2
Jorg Woehl
Jorg Woehl 2021년 3월 8일
Hi Jonathan, just to make sure I understand your problem correctly: I can see how B is supposed to act on the cell array A if the number listed in B is found in the cell of A. And if it is not found in A (such as in [20]), are the next cells of A simply skipped until the number listed B (i.e. 59 in your example) is found in a subsequent cell of A?
Jonathan Cheong
Jonathan Cheong 2021년 3월 8일
Hi Jorg,
Yes that's correct

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2021년 3월 9일
With some bold guessing:
A = {[1, 2, 3, 4, 5], [20], [57, 58, 59, 60]};
B = [3, 59];
iB = 1;
for iA = 1:numel(A) % Loop over elements of A
index = find(A{iA} == B(iB)); % Search current B
if ~isempty(index) % If a match is found:
A{iA} = A{iA}(1:index); % Crop current A
iB = iB + 1; % Select next B
end
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by