Hello,
I have a data array like:
x = [0, 0, 45, 43, 42, 41, 0 , 0 , 0, 0, 45, 43, 42, 41 ];
In my data points, 2 times my data became non-zero. Which I call them an "event".
I want my code to calculates the number of events as "2".
and calculate the sume of numbers at each event.
I'd appreciate your help at this matter.
Thanks,

 Respuesta aceptada

Matt J
Matt J el 10 de Jul. de 2019
Editada: Matt J el 10 de Jul. de 2019

0 votos

Apply the suggestions in this thread to A=logical(x)+1.

4 comentarios

Sarah Aimen
Sarah Aimen el 10 de Jul. de 2019
Thanks Matt for your prompt response! Actually my problem is much more complicated than this.I just simplified it. I attached a screenshot of my actual data.
I wrote this code,
x = [1, 2, 45, 43, 42, 41, 6 , 2, 7];
for i = 1 : length(x);
if x(i) > 10;
i
end
i = i + 4;
end
I want my code to sweep data, as soon as it saw a data greater than 10, print it and skip 4 points after it, then continue sweeping to find the next point greater than 10.
Ideal answer: 45
What this code gives: 45, 43, 42, 41
Matt J
Matt J el 10 de Jul. de 2019
Just use a while loop, then:
x = [1, 2, 45, 43, 42, 41, 6 , 2, 7];
i=0;
while i<length(x)
i=i+1;
if x(i) > 10;
x(i)
i = i + 3;
end
end
Matt J
Matt J el 10 de Jul. de 2019
Sarah's comment relocated here
Perfect! Works very well!
Thank you very much, Matt!
Matt J
Matt J el 10 de Jul. de 2019
You're welcome. Please Accept-click the answer, though, to certify that we solved it.

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 10 de Jul. de 2019

0 votos

Try this (requires the Image Processing Toolbox, which you probably already have):
x = [0, 0, 45, 43, 42, 41, 0 , 0 , 0, 0, 45, 43, 42, 41 ];
% Count number of non-zero regions: (requires Image Processing Toolbox)
[~, numNonZeroRegions] = bwlabel(x)
% Compute sum of values
props = regionprops(x>0, x, 'PixelValues');
sumOfValues = sum(vertcat(props.PixelValues), 2)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 10 de Jul. de 2019

Respondida:

el 10 de Jul. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by