Find the first element that satisfies a condition on 3D data
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Elin Jacobs
am 24 Mai 2021
Kommentiert: Elin Jacobs
am 25 Mai 2021
I'm trying to use a logical expression to find the first instance of a condition being met on a 3d matrix.
I have this dummy code for a 1d array that works the way it should:
d = randi([0 50], 100, 1);
dd = sort(d, 'descend');
pa = sum(dd);
dc = cumsum(dd);
%find row where cumsum is 50% of sum
g = find(dc >0.5*pa,1);
However, I can't get it to work on my 3d data of longitude, latitude and time (360,150,365). I want to find the n'th timestep (in the third dimension) where SC is equal to or greater than 1/2 of the sum at each latitude-longitude pair and write that to a new matrix (2d, 360X150). My current attemp is as follows:
prec = ncread(filename_p, 'Prec_gs'); % 360x150x365 matrix
p_annual(:,:) = sum(prec,3,'omitnan'); % 360x150 matrix
S = sort(prec,3,'descend'); % 360x150x365 matrix
SC = cumsum(S,3); % 360x150x365 matrix
N50(:,:) = find(SC(:,:,:) >= 0.5.*p_annual(:,:), 1);
The last row isn't working because the the dimensions are not the same (3d vs 2d), I have tried making p_annual a 3d matrix of same size as SC but still no luck.
Any thoughts on how to solve this?
2 Kommentare
Akzeptierte Antwort
Matt J
am 24 Mai 2021
S = sort(prec,3,'descend'); % 360x150x365 matrix
SC = cumsum(S,3,'omitnan'); % 360x150x365 matrix
p_annual = SC(:,:,end);
[~,N50] = max(SC >= 0.5.*p_annual,[], 3);
6 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Operators and Elementary Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!