Info

此问题已关闭。 请重新打开它进行编辑或回答。

Find the vector of medians

1 次查看(过去 30 天)
Jayanta Deb
Jayanta Deb 2017-8-15
关闭: MATLAB Answer Bot 2021-8-20
say I have a vector
v =[1,2,3,-1,-2,-3,4,5,6,-7,-6,-3,-5,-4,...]
I want to make a vector which consists of the values of the occurrence of negative values.
when the pointer is at v[4] till the last negative value v[6]
next step I need to take the median from the vector v of the first occurrences of the negative values
Vm = [-2] % first occurrence
accordingly i want to repeat the procedure for v[10] till v[13] and determine the median again.
And add it up to the vector Vm = [-2,-3] and so on. The output should be like this: vm = [(median(v(4) to v(6)), (median(v(10) to v(13))),...]
so for this example the vector should look like this vm = [-2,-5,...]
Can you please help me out?
Thanks in advance
  2 个评论
Stephen23
Stephen23 2017-8-15
@Jayanta Deb: please edit your question and show the expected output.
Jayanta Deb
Jayanta Deb 2017-8-15
I have edited the question. You can see.
Thanks

回答(1 个)

Guillaume
Guillaume 2017-8-15
One way:
v = [1,2,3,-1,-2,-3,4,5,6,-7,-6,-3,-5,-4];
transitions = find(diff([0, v<0, 0]));
result = arrayfun(@(s,e) median(v(s:e)), transitions(1:2:end), transitions(2:2:end)-1)
Note that the second negative sequence in your example is [-7,-6,-3,-5,-4] so its median is -5.

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by