Delete Individual Elements in a Cell Array

Delete Individual Elements in a Cell Array

I have a 100 X 1 (n1) cell array with each cell holding indices of a bigger data set(100 X 100, n2). I made a nested loop in order to access each individual element(index) and compare the values of another data set with these indices with a if condition. If the condition succeeds, I want to delete that element from the original cell array into a new cell array. However when I set the element to [] in matlab, the value of the cell array does not change. The code is below:

for i = 1:length(n1)
    for j = 1:length(n1{i, 1})
        if n2(i,n1{i,1}(1,j)) > n3(i) && n2(i, n1{i,1}(1,j)) > n4(n1{i, 1}(1, j))
            n1{i,1}(1,j) == [];
        end                          
    end
end
2

1 Answer

I take that n1(i,1) is always a row vector so you should use,

n1{i,1}(j) = [];

If n1(i,1) is not a column or row then removing an element from middle would be impossible.

for example:

A = {[1 2 3],[5 8 9]}
A{1,2}(1,2) = []

gives the error: A null assignment can have only one non-colon index.

But A{1,2}(2) = [] is okey.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.