How do I find the row number where the first zero occurs and last zero occurs in an excel spreasheet?
Afficher commentaires plus anciens
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)
Thorsten
le 20 Mai 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 commentaires
Oliver Kohout
le 20 Mai 2015
Thorsten
le 20 Mai 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
le 20 Mai 2015
Modifié(e) : Andrei Bobrov
le 20 Mai 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]);
Catégories
En savoir plus sur Data Import from MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!