简体   繁体   中英

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

I have a 4-D matrix. The dimensions are longitude, latitude, days, years as [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.

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.

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. 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%.

I'm guessing the same value you are getting for r is 39; the number of elements in the top 2% of your 122x16 matrix (ie 1952 elements).

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 ).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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