Borrar filtros
Borrar filtros

Calculate the difference between two points extracted from a loop?

4 visualizaciones (últimos 30 días)
A is an array of [441000,1]. The values contained in this array are in the order:
a =
0
0
0
0
1
1
1
1
0
0
0
0
1
1
1
1
1
0
0
0
I have to find the difference between every starting 1 (i.e 1 after ever 0) and till the next xth position 1. (the position of first 1 - the position of xth 1 after the first series of 1).
So, first difference would be 101-1200 and then 1090-2202, 2200-3200 and so on. Kind of creating an overlap for every cutout.
In the data sample, there are approximately 1300 zeros after 100 ones. So I have to cut out the first one till the next 4th or 5th one and calculate the difference.
It feels like a simple code, but I am totally lost atm.
Thanks for your help!
  2 comentarios
Guillaume
Guillaume el 19 de Jul. de 2016
Can you please use valid matlab syntax for your example instead of some obscure notation that only you knows the meaning of.
What does the "difference between every starting 1 and till the next xth position 1" mean? The difference between 1 and 1 is always going to be 0, regardless of their position
What does "xth 1" mean? What is the value of x?
What your asking sounds like it could indeed be very simple (probably does not even need a loop) but it's really not clear at the moment.
automycer
automycer el 19 de Jul. de 2016
The difference is calculated based upon their indices. Suppose, first 1 is at index 'x1' and the next 1 is at 'x2'. I need the difference of x1-x2 (based on the sample data, this would be 200-1300)
I think a while loop can solve this. The algorithm would be, x1 -(x2+10).

Iniciar sesión para comentar.

Respuesta aceptada

Guillaume
Guillaume el 20 de Jul. de 2016
Assuming a is a vector of 0 and 1 exclusively. Finding the index of each 1 immediately following a zero is trivial: compute the diff of the vector. The [0 1] transition is the only one that results in a diff of 1. ([1 1] and [0 0] is 0, and [1 0] is -1):
start1loc = find(diff(a)) + 1; %add 1 to the result of find since find finds the position of 0 in [0 1]
If you want to also detect a 1 at the beginning of the vector (where it obviously does not follow a zero):
start1loc = find(diff([0; a])); %no offset needed in this case
To compute the difference between these indices, you use another diff
result = diff(start1loc)

Más respuestas (1)

Thorsten
Thorsten el 19 de Jul. de 2016
x(101:103) = 1;
x(1090:1091) = 1;
x(1200:1201) = 1;
x(2201:2203) = 1;
x(3200) = 1;
istart = find([0 diff(x) == 1]);
i1 = find(x == 1);
iend = nan(1, numel(istart));
for i = 1:numel(istart)
i1g = i1(i1 > istart(i));
if numel(i1g) >= 5
iend(i) = i1g(5);
end
end
[istart' iend']
  2 comentarios
automycer
automycer el 19 de Jul. de 2016
Editada: automycer el 19 de Jul. de 2016
Thanks for your answer. This works for the above data but I guess in my case its a fairly simple problem.
y[m,1] (as shown in the question). Now, I need to find the index of first '1' after every zero and then run a loop like:
i = length(y);
while (i>0)
{
if(y(i) == 1 && y(i-1,1) == 0)
{
% store the indices in a new array.
}
i=i-1;
end
}
The new array will store indices of all '1s' at the start of a set. Thanks! (not sure about the structure of the arbitrary code).
Thorsten
Thorsten el 20 de Jul. de 2016
If I understood you correctly, this will do the task:
istart = find([0 diff(x) == 1]);
iend = find([diff(x) == -1]);
% store the indices in a cell array
Cidx = arrayfun(@(i) istart(i):iend(i), 1:numel(iend), 'UniformOutput', false);

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by