How do I find the row number where the first zero occurs and last zero occurs in an excel spreasheet?

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.

Réponses (2)

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 commentaires

Sorry i didnt make it clear, basically the excel spreadsheet goes like
[ 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]
So I want to find the row where the first set of zero's ends? And then find the row where the next set begins and so on
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);

Connectez-vous pour commenter.

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]);

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by