loop implementation with two conditions

조회 수: 2 (최근 30일)
A VP
A VP 2018년 1월 3일
답변: Guillaume 2018년 1월 3일
I have a time series data sampled at 100 ms. I would like to pick samples every 1 sec or when difference between 2 consecutive samples is more than 10 and then implement a logic inside this condition. How do I use both these together as an 'if' condition?
Thanks!
  댓글 수: 2
Guillaume
Guillaume 2018년 1월 3일
"How do I use both these together as an 'if' condition?"
Most likely, if is the wrong tool. Can you give an example of "the logic inside this condition"?
A VP
A VP 2018년 1월 3일
I need not necessarily stick to an if-loop. Is there an alternate way to implement it?
The logic inside the condition is to store the samples in a new array.

댓글을 달려면 로그인하십시오.

답변 (1개)

Guillaume
Guillaume 2018년 1월 3일
Something like this would work:
samples = randi([0 20], 1, 1000); %demo data
indices1s = 1:100:numel(samples); %indices at every 1 s
indicesdiff = find(abs(diff(samples)) > 10); %indices of 1st sample when consecutive sample difference is greater than 10.
newsamples = samples(unique([indices1s, indicesdiff, indicesdiff+1])); %indicesdiff+1 to get the 2nd of the two samples when diff is greater than 10
It's all vectorised operations, so no if needed.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by