Modify array zeros detect
Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Ältere Kommentare anzeigen
Hello Community,
there is a vector lets say:
A = [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0];
What I want to know are the last indices when it is going to switch to 1. Only this, is not a problem. Additionally the indices should be read out only if the length of the zeros are longer than lets say 12 positions.
In this array for example: 19, 58 etc.
Tried it with combination of for, if loop and find (A), but it was not 100% the expected value.
Thanks a lot!
1 Kommentar
Image Analyst
am 24 Dez. 2016
Can you fill out the "etc." in "19, 58 etc." - what are all the indices in your example. And clarify "last indices when it is going to switch to 1." So does that mean the last index that is a zero right before the array starts being 1's as long as that prior stretch of zeros was 13 or more elements long?
In your example, element 19 is a zero right in the middle of a long stretch of 0's. It is not the last zero before it switches to 1, so can you explain why 19 is part of your desired answer?
Antworten (2)
Walter Roberson
am 23 Dez. 2016
A = [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0];
idx = strfind(A, [zeros(1, 12), 1]) + 11;
4 Kommentare
N/A
am 23 Dez. 2016
Walter Roberson
am 23 Dez. 2016
"the last index (92) is not mentioned in the solution, although it has 12 zeros"
It does not switch to 1 there, which was a requirement of your question.
The strfind() used here is the same as for strings https://www.mathworks.com/help/matlab/ref/strfind.html . It is not documented there, but it happens that strfind works with numeric values as well as characters, For example if you had done
S = sprintf('%d',A);
then you could clearly use
strfind(S, '0000000000001')
to find the position of a 1 that follows 12 zeros.
N/A
am 23 Dez. 2016
Walter Roberson
am 23 Dez. 2016
If you want to match on any non-zero value, then
A = [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 7 1 1 1 1 1 1 1 1 1 1 1 5 1 1 19 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -4 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0];
idx = strfind(A ~= 0, [zeros(1, 12), 1]) + 11;
If you want to match on a specific non-zero value then
A = [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 7 1 1 1 1 1 1 1 1 1 1 1 5 1 1 19 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -4 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0];
target = 8;
idx = strfind(A, [zeros(1, 12), target]) + 11;
If you want to find a match at the end as well,
A = [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 7 1 1 1 1 1 1 1 1 1 1 1 5 1 1 19 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -4 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0];
target = 8;
idx = strfind([A, target], [zeros(1, 12), target]) + 11;
Roger Stafford
am 24 Dez. 2016
You could also use the following method. A is a row vector with 1’s and 0’s, and m is the least number of successive 0’s before switching to a 1 that have that last 0 position recorded in idx.
f = find(diff([1,A,0]~=0));
f1 = f(1:2:end);
f2 = f(2:2:end);
idx = f2(find(f2-f1(1:end-1)>=m))-1; % <-- The desired indices
Diese Frage ist geschlossen.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!