简体   繁体   English

查找MATLAB中每个单元的阈值以上元素的频率

[英]Find frequency of elements above a threshold for each cell in MATLAB

I have a 4-D matrix. 我有一个4-D矩阵。 The dimensions are longitude, latitude, days, years as [17,14,122,16]. 维度是经度,纬度,天,年,为[17,14,122,16]。 I have to find out frequency of values above 98 percentile for each cell so that final output comes as as array of 17x14 containing number of occurrence of values above a 98 percent threshold. 我必须找出每个单元格中98%以上的值的频率,以便最终输出为17x14数组,其中包含出现次数超过98%阈值的值。

I did something which gives me a matrix 17x14 of values associated with 98 percentile for each cell but I am unable to determine the frequency of occurrences. 我做了一些事情,使我获得了一个矩阵17x14的值,与每个单元格的98个百分位数相关,但我无法确定出现的频率。

k=0;
p=cell(1,238);
r=cell(1,238);

for i=1:17
   for j=1:14
      n=m(i,j,[1:122],[1:16]);
      n=squeeze(n);
      k=k+1;
      q=prctile(n(:),98);
      r{k}=nansum(nansum(n>=q));
      p{k}=q;
   end
end

This code gives matrix p fine but matrix r contains same values for all cells. 此代码使矩阵p精细,但矩阵r对于所有单元格都包含相同的值。 How can this be possible? 这怎么可能? What am I doing wrong with this? 我在做什么错呢? Please help. 请帮忙。

By definition, the frequency of values above the 98th percentile is 2%. 根据定义,高于第98个百分点的值的频率为2%。

I'm guessing the same value you are getting for r is 39; 我猜想你得到的r 39。 the number of elements in the top 2% of your 122x16 matrix (ie 1952 elements). 122x16矩阵顶部2%的元素数量(即1952个元素)。

r = 0.02*1952; 

r = 
      39.040 

Your code is verifying the theoretical value. 您的代码正在验证理论值。 Perhaps you are thinking of a different question? 也许您在想一个不同的问题?

Here's a simulated example, using randomly generated (uniform distribution) from 0 to 100 for your data ( n ). 这是一个模拟示例,对数据( n )使用从0到100的随机生成(均匀分布)。

p=cell(1,238);
r=cell(1,238);
for i=1:17
    for j=1:14
        %         n=m(i,j,[1:122],[1:16]);
        %         n=squeeze(n);

        % After you do n=squeeze(n), it gives 2-D matrix of 122x16
        % dimensions.
        n = rand(122,16)*100;  % simulation for your 2-D matrix
        k=k+1;
        q=prctile(n(:),98);
        r{k}=nansum(nansum(n>=q));
        p{k}=q;
    end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM