从Matlab中向量的负值的出现中找到中值向量 [英] Find the vector of medians from the occurrences of the negative values of the vector in matlab
问题描述
说我有一个向量
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.
当指针位于v[4]
时,直到最后一个负值v[6]
When the pointer is at v[4]
till the last negative value v[6]
下一步,我需要从负值首次出现的向量v中获取中位数
Next step I need to take the median from the vector v of the first occurrences of the negative values
Vm = [-2] % first occurrence
因此,我想对v[10]
重复该过程,直到v[13]
,然后再次确定中位数.
Accordingly I want to repeat the procedure for v[10]
till v[13]
and determine the median again.
并将其添加到向量Vm = [-2,-3]
中,依此类推.输出应如下所示:vm = [(median(v(4) to v(6)), (median(v(10) to v(13))),...]
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))),...]
因此,在此示例中,向量应如下图所示vm = [-2,-4,...]
So for this example the vector should look like this vm = [-2,-4,...]
推荐答案
v =[1,2,3,-1,-2,-3,4,5,6,-7,-6,-3,-5,-4]; %Given vector
%Start and end of intervals. strfind is applicable on numeric data as well.
pos = [1,v>=0,1]; start = strfind(pos,[1,0]); last = strfind(pos,[0,1])-1;
n=length(start); %Number of chunks of the negative values
vm = zeros(1,n); %Pre-allocation of vm
for k=1:n
vm(k) = median(v(start(k):last(k))); %Finding required vector of medians
end
如果没有负值,则vm
将作为空向量返回.
If there is no negative value, vm
will be returned as an empty vector.
关于语句"我想制作一个由负值出现的值组成的向量.",看来这不是您的实际目标,但您可以用v(v<0)
找到它.
Regarding the statement, "I want to make a vector which consists of the values of the occurrence of negative values.", it seems that it is not your actual goal but you can find that with v(v<0)
.
这篇关于从Matlab中向量的负值的出现中找到中值向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!