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.

 采纳的回答

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]
Y = 1×25
2.6597 2.6565 2.6565 2.6562 2.6623 2.6613 2.6600 2.6204 2.6143 2.6062 2.6008 2.6004 2.5908 2.5917 2.5905 2.5869 2.5850 2.5830 2.5853 2.5782 2.5776 2.5808 2.5789 2.5731 2.5756
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]
X = 1×33
0 0 0 0 0 0 0 46.2400 46.2400 46.2400 46.2400 46.2400 46.0000 46.0000 46.0000 46.0000 46.0000 45.9900 45.9900 45.9900 45.9900 45.9900 46.0000 46.0000 46.0000 46.0000 46.0000 46.0000 46.0000 46.0000
idx = diff(X~=0)>0;
out = Y(idx)
out = 2.6600

6 个评论

That works perfectly! Thank You! Also, KALYAN ACHARJYA thank You for trying to help!
Regards
Stephen, I have one more question regarding this topic.
Now, I want to have every value of array Y that corresponds to every first (and only first, not each) "46" that appears (pulse beginning).
How will the condition look like?
@Kacper Witasinski: do you want to match exactly the value 46, or any value that is approximately 46 at the start of the pulse? (e.g. 46.24)
Any value that is at the start of the pulse, sir.
out = Y([false,idx])
out = 2.6204
Works, but one note for people dealing with same problem in the future. Matlab returned an error "Dimensions of matrices being concatenated are not consistent." After I transposed idx everything works perfectly.
Regards and thank You once more Stephen.

请先登录,再进行评论。

更多回答(2 个)

Steven Lord
Steven Lord 2021-2-22
Since you seem to be describing change point detection, see the ischange function and/or the Find Change Points task in the Live Editor.
"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 个评论

No, it doesnt work as I want. The value of "pulse" is not always 46 during the duriation of it. It has some errors, for example in 10 out of 1000 values like 45,99 or 46,03 may appear. By the way, using your code resulting Y is 1x4 array of zeros.
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 ]
The values of X array given by me are really exemplary values that should give general scope of the behaviour.
Corresponding values of Y are more or less constant (the difference is in third/fourth number after the decimal point) for zeros of X array. When the pulse happen, the values are decreasing in exponential way and then values are increasing in exponential way when pulse ends. I can show You how graphs looks like to give You the idea.
So I will write once again. For majority of time the values of array X are equal to 0. At some instant, there is a pulse and the values of array Y decrease. I need the exact values of array Y that corresponds to the last zero value of array X before the pulse happen for the whole length of array (Imagine that there are 10 pulses).
Small fragment of my data:
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 and so on]
Please, dont mind comas.

请先登录,再进行评论。

类别

帮助中心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!

Translated by