Remove all number that comes after in cell array

1 view (last 30 days)
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 Comments

Sign in to comment.

Accepted Answer

Jan
Jan on 9 Mar 2021
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

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by