Computation on arrays using loops
이전 댓글 표시
Hello,
I want to create code that will do operations on specified values from the arrays. I have array X and array Y, where array Y is X-dependent. Array X looks more or less like this: [0 0 0 0 0 0 0 0 0 0 0 45.9 45.98 45.99 46 46 46 46 0 0 0 0 0 0 0 ...], it represents pulse-like behaviour. I want matlab to save in workspace the values of array Y that corresponds to the every last "0" of the array X before the pulse ''46'' happens on the length of the whole array.
I expect that for/while loop has to be applied, but I am not sure which one and how the statements should look like. I would appreciate any help. Thank You. I am using matlab2020b version.
채택된 답변
추가 답변 (2개)
Steven Lord
2021년 2월 22일
1 개 추천
Since you seem to be describing change point detection, see the ischange function and/or the Find Change Points task in the Live Editor.
KALYAN ACHARJYA
2021년 2월 19일
편집: KALYAN ACHARJYA
2021년 2월 19일
"I want matlab to save in workspace the values of array Y that corresponds to the every last "0" of the array X before the pulse ''46'' happens on the length of the whole array."
Is this? Note for this problem loop can be avoided
idx=find(diff(X)==-46)+1
This idx gives the indices of x, where x equal to zero and it's corresponding previous data to be equal to 46
Afterwards
Y=X(idx)
댓글 수: 3
Kacper Witasinski
2021년 2월 19일
KALYAN ACHARJYA
2021년 2월 19일
편집: KALYAN ACHARJYA
2021년 2월 19일
Yes as per my code gives transition indices (from 46 to 0 only)
Can you clarify again? In this X example, what would be the Y
X=[0 0 0 0 0 0 0 0 0 0 0 45.9 45.98 45.99 46 46 46 46 0 0 0 0 0 0 0 ]
Kacper Witasinski
2021년 2월 19일
편집: Kacper Witasinski
2021년 2월 19일
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!