0
votes

I want to delete some cell(s) from cell array in Matalb on run time in nested for-loops, so that I want permanently delete cell and on next iteration, It will not consider that cell infect its next cell as its place, How I can perform this in Matlab, or any other suggestions to avoid cell array in this case. My cell array M contains objects information and P is the position values.

My Code:

for ii=1:1000
    for jj=1:20
        M{jj}=P{jj}(ii,:);
        if (P{jj}(ii,:) ~= 0)
            %here I want to delete M(jj) or M{jj} and also P(jj) or P{jj}
            % My try  M(jj) = []; M=  M(~cellfun('isempty',M));
            %but it gives error when the next iteration starts.
        end
    end
end
1
I think it won't reach inside the conditional statement if (M{jj} == 0 ) because the way you are assigning M{jj} before the conditional statements. - Divakar
That was just an example, pls check edited Code, I want to delete some jj like jj=2 in this case. - user3593525

1 Answers

0
votes

I would suggest using while instead of for for this special case -

ii=1;
jj=1;

jj_limit = 20;
while ii<=1000
    while jj<=jj_limit
        M{jj}=P{jj}(ii,:);
        if (P{jj}(ii,:) ~= 0)
            P(jj)=[];
            M(jj)=[];
            jj_limit = jj_limit-1;
            continue;
        end
        jj = jj+1;
    end
    ii = ii+1;
end