MATLAB Answers

How many zeros do I have before a specific sting variable?

3 views (last 30 days)
antonet
antonet on 15 Aug 2012
Dear all
I have
A={
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
' b1'
' b1'
' b1'
' b1'
' b1'
'b1'
' b1'
[0]
[0]
[0]
'b1'
' b1'}
I have this cell vector. This vector has a characteristic. Immediately after the first zeros the letter 'b1' begins. So I would like to count the zeros before the first 'b1' that starts to appear in this vector. In this case I have 39 zeros. Is it possible to calculate this number using a Matlab code?
thanks

  0 Comments

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 15 Aug 2012
yourzeros = find(cellfun(@ischar,A),1,'first')-1;

  0 Comments

Sign in to comment.

More Answers (3)

Matt Fig
Matt Fig on 15 Aug 2012
Edited: Matt Fig on 15 Aug 2012
T = strfind(cellfun(@(x) ~x(1),A).',[1 0])
Notice that T will show you the index of every zero that is followed by a nonzero. So you want T(1).

  0 Comments

Sign in to comment.


Azzi Abdelmalek
Azzi Abdelmalek on 15 Aug 2012
min(find(cellfun(@(x) ~isnumeric(x),A)))-1

  0 Comments

Sign in to comment.


Oleg Komarov
Oleg Komarov on 15 Aug 2012
Another solution:
pos = strfind([A{:}],'b')-1
pos(1)

  2 Comments

Andrei Bobrov
Andrei Bobrov on 16 Aug 2012
Hi Oleg! may be in this case so?
pos = strfind([A{:}],' ')-1
pos(1)

Sign in to comment.

Sign in to answer this question.

Tags