delete first x number of rows from a cell array

5 views (last 30 days)
I have a 2000*4 cell array. The 4th column is an ascending numeric values from 0 to 30. I do not know how many rows have 0 value in 4th column, and I would like to delete those rows from cell array. I wrote the following code with the loop, which works well. But is there any efficient way to avoid the for-loop?
[m,n]=size(A);
count = 0;
for i=1:m
if isequal(A{i,4},0)
count = count+1;
end
end
A = A(count+1:m,:)
Thank you in advance

Accepted Answer

Stephen23
Stephen23 on 25 Mar 2016
>> C = num2cell([randi(9,6,3),[0;0;0;1;2;3]])
C =
[5] [8] [6] [0]
[2] [3] [5] [0]
[2] [9] [4] [0]
[3] [4] [8] [1]
[8] [2] [6] [2]
[3] [3] [5] [3]
>> C([C{:,4}]>0,:)
ans =
[3] [4] [8] [1]
[8] [2] [6] [2]
[3] [3] [5] [3]

More Answers (1)

Jos (10584)
Jos (10584) on 25 Mar 2016
Each cell in the 4th row of A should contain a scalar, being 0 or another value
tf = [A{:,4}] ~= 0
Aout = A(tf,:)

Categories

Find more on Multidimensional Arrays 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!