Replace yearly max value with next maximum value

조회 수: 3 (최근 30일)
Kara Sutcliffe
Kara Sutcliffe 2020년 12월 15일
편집: Image Analyst 2020년 12월 15일
Hi,
I am dealing with a very large table, that has many of the same values in a column that I am interested in. I am defining a threshold by calculating the mean+std of the maximum value of each year. Then iteratively, I am removing the max value of each year and replacing it with the next highest, if the max value is above the threshold, i.e., an outlier.
I have used the following to extract the maximum yearly values
YearlyMax = arrayfun( @(yy) max(WSabund_CA(years==yy)), unique(years));
Then the following loop to do the process I am trying to achieve. Please note this is also inside a while loop so that it stops running when there are no more outliers.
h = size(YearlyMax);
for z = 1:h(1);
if YearlyMax(z) > threshold;
disp("outliers exist")
if YearlyMax(z) == YearlyMin(z)
disp("Max is equal to the Min")
YearlyMax(z) = YearlyMax(z);
else
CAsector(CAsector.WHITESYNDROME == YearlyMax(z),:) = [] ;
disp("Max outlier is removed")
end
r = size(CAsector);
disp(r)
end
end
My issue is that, I do not want to exclude a maximum value that is deemed an outlier, if it is equal to a yearly minumum value, which is calculated that same as the maximum values with the arrayfun command. But the statement under the else in the if loop, removes all rows with a value that is equal to that Yearly Max, so it may be removing more than one value, as there may be more than one of the YearlyMax(z) value. So the issue is that it is removing the maximum value for the year but along the way it is also removing other values, that are not wanting to be removed.
I need a way of removing only the YearlyMax value and row it is associated with in the original data set CAsector.
I figure it might be completely changing the way I have calculated the max values and how I remove them, but I am completely stuck, so if anyone has any suggestions it would be very appreciated.
Thank you.
  댓글 수: 1
Image Analyst
Image Analyst 2020년 12월 15일
편집: Image Analyst 2020년 12월 15일
Hard to visualize without an example. Please attach your table or array (small sample of it) in a .mat file or give code to create it. And give what you expect the output to be. Actually I guess you must have an array, not a table because I see you're using arrayfun() and that does not work on tables.
By the way I don't think it's a wise idea to be shrinking the size of the array at the same time you're indexing through it. Let's say you were on 2 and then delete it. Then you're now on index 3 but the next iteration will index the new index 3, which is the old index 4, not the old index 3 like you thought you were going to process. It's usually better to just keep a log of what indexes need to be deleted and then delete then AFTER the loop, not DURING the loop.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by