Computation on arrays using loops
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Kacper Witasinski
el 19 de Feb. de 2021
Editada: Kacper Witasinski
el 23 de Feb. de 2021
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.
0 comentarios
Respuesta aceptada
Stephen23
el 19 de Feb. de 2021
Y = [2.659717,2.656496,2.656496,2.656173,2.662294,2.661328,2.660039,2.620416,2.614295,2.606242,2.600765,2.600443,2.590779,2.591745,2.590457,2.586913,2.58498,2.583048,2.585303,2.578216,2.577571,2.580793,2.57886,2.573061,2.575638]
X = [0,0,0,0,0,0,0,46.24,46.24,46.24,46.24,46.24,46,46,46,46,46,45.99,45.99,45.99,45.99,45.99,46,46,46,46,46,46,46,46,46,46,46]
idx = diff(X~=0)>0;
out = Y(idx)
Más respuestas (2)
Steven Lord
el 22 de Feb. de 2021
Since you seem to be describing change point detection, see the ischange function and/or the Find Change Points task in the Live Editor.
0 comentarios
KALYAN ACHARJYA
el 19 de Feb. de 2021
Editada: KALYAN ACHARJYA
el 19 de Feb. de 2021
"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 comentarios
KALYAN ACHARJYA
el 19 de Feb. de 2021
Editada: KALYAN ACHARJYA
el 19 de Feb. de 2021
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 ]
Ver también
Categorías
Más información sobre Characters and Strings 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!