How do I find the row number where the first zero occurs and last zero occurs in an excel spreasheet?
Show older comments
For example, the spreadsheet values in a certain column are as follows:1 4 5 8 0 0 0 0 6 5 1 4 6 0 0 0 0 0 4 5 6
so far i have this:
A=find((data{1,1}(:,5))>=0,1,'first') %find row of first 0 value for single jump
A2=find((data{1,1}(A,5))<0,1,'first') %find row of last 0 value for single jump
but where the 'A' is on the second line I want it to search from A to the last value in that column, not the entire column as stated using ':' in the line above.
Answers (2)
Thorsten
on 20 May 2015
x = [1 4 5 8 0 0 0 0 6 5 1 4 6 0 0 0 0 0 4 5 6];
first0 = find(x == 0, 1, 'last');
last0 = find(x == 0, 1, 'first');
2 Comments
Oliver Kohout
on 20 May 2015
Thorsten
on 20 May 2015
Ok. Please use this:
A = [ 5 6 7 4 6 0 0 0 0 5 6 4 3 5 6 7 0 0 0 0 0 4 5 6 0 0 0 0 6 6 4 4 0 0 0 0]
d = diff(A == 0);
ind_firstzero = find(d == 1) + 1;
ind_lastzero = find(d == -1);
Andrei Bobrov
on 20 May 2015
Edited: Andrei Bobrov
on 20 May 2015
a = [ 5 6 7 4 6 0 0 0 0 5 6 4 3 5 6 7 0 0 0 0 0 4 5 6 0 0 0 0 6 6 4 4 0 0 0 0];
t = a == 0;
ii = strfind([0,t],[0, 1]);
x = zeros(size(a));
x(ii) = 1;
cx = cumsum(x).*t;
b = accumarray(cx(:)+1,(1:numel(a))',[],@(x){[x(1),x(end)]});
idx_first_last_zero = cat(1,b{2:end});
or just
a = [ 5 6 7 4 6 0 0 0 0 5 6 4 3 5 6 7 0 0 0 0 0 4 5 6 0 0 0 0 6 6 4 4 0 0 0 0];
ii_first = strfind([0,t],[0, 1]);
ii_last = strfind([t,0],[1, 0]);
Categories
Find more on Data Import from MATLAB 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!