Get every first value above a treshold out of an array

1 visualización (últimos 30 días)
Mike Mierlo van
Mike Mierlo van el 8 de Dic. de 2019
Comentada: Mike Mierlo van el 9 de Dic. de 2019
Hi guys, Lets say I have an array with elements with
A = [1 2 3 4 5 6 2 3 4 5 6 3 4 5 6 7 8 1 2 1 2 1 2 3 4 5 6]
Now I want to have only the first values that are above the treshold 4.5 when reading from left to right. So reading from left to right when an element is lower than 4.5 it is a 0, when it is higher than 4.5, only the first element is a 1. When the next element is also higher than 4.5 it should be a zero. Only when the value of an element drops below 4.5 the next element above 4.5 should again be a one.
The result should be:
B = [0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0]
Please help me with the magic between A and B.
  2 comentarios
Image Analyst
Image Analyst el 8 de Dic. de 2019
Is this homework (it looks like it)?
Mike Mierlo van
Mike Mierlo van el 9 de Dic. de 2019
Hi. It is not homework. It is a simplified array. The actual array contains measurements of degradation of a component. This fails at a certain treshold. After that a new component is build in. The acutal array is way to big to share here, so I asked the question with a simplified code. I am going to try to fix it with the hints. If not, I'll be back.

Iniciar sesión para comentar.

Respuesta aceptada

dpb
dpb el 8 de Dic. de 2019
Editada: dpb el 9 de Dic. de 2019
B=([0 diff((A>trsh)-1)]==1);
For comparison,
>> [A;B]
ans =
Columns 1 through 27
1 2 3 4 5 6 2 3 4 5 6 3 4 5 6 7 8 1 2 1 2 1 2 3 4 5 6
0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0
>>

Más respuestas (1)

Image Analyst
Image Analyst el 8 de Dic. de 2019
Looks like homework so we're only giving hints. Hint:
da = [0, diff(A > 4.5)]
and then look up strfind() and zeros() to help you get the answer in the final 3 lines of code.
  1 comentario
dpb
dpb el 8 de Dic. de 2019
"...so we're only giving hints."
Oh, I didn't see this in time...sorry! :)

Iniciar sesión para comentar.

Categorías

Más información sobre Logical 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