简体   繁体   中英

How do I find maximum change point in data columns and write this with a conditional statement

MATLAB. I have a (2559 x 16 x 3) array named ydisplacements. I use the 'ischange' command with a specified threshold, in a loop to find points of rapid change in the data in each column, for all three planes. This I record in a logical array TF(2559 x 16 x 3) in which '1' denotes points of sharp change. I want to have TF such that in each of its columns only the most extreme change from 'ydisplacements' is recorded in it as logical 1. In other words, everything else is a logical 0, I want only one logical 1 for each column of TF. This I can do easily by increasing the threshold till my desired outcome. But my question is how I can automate this so that for each column of ydisplacements(:,i,j), the threshold keeps increasing by a predetermined value until the desired outcome of only one logical 1 is achieved in TF, before moving to the next index. I have tried this code below. Any help is appreciated. Thank you..

increment = 100;
for i = 1:size(ydisplacements,2);
    for j = 1:size(ydisplacements,3);
        threshold = 100;
        TF(:,i,j) = ischange(ydisplacements(:,i,j),'Linear','Threshold',threshold);
        if sum(TF(:,i,j) == 1)>1; 
            threshold = threshold + increment;
            TF(:,i,j) = ischange(ydisplacements(:,i,j),'Linear','Threshold',threshold);
        else
            threshold = threshold;
            TF(:,i,j) = ischange(ydisplacements(:,i,j),'Linear','Threshold',threshold);
        end
    end
end

Not 100% sure without an example data. But it is very likely using if-else statment will only increment the threshold once. What I would do is using a while-loop as a unlimited iterative if statement. Something like:

increment = 100;
for i = 1:size(ydisplacements,2);
    for j = 1:size(ydisplacements,3);
        threshold = 100;
        TF(:,i,j) = ischange(ydisplacements(:,i,j),'Linear','Threshold',threshold);
        while sum(TF(:,i,j) == 1)>1  % Iterate always sum(TRUEs) > 1
            threshold = threshold + increment;
            TF(:,i,j) = ischange(ydisplacements(:,i,j),'Linear','Threshold',threshold);
         end
    end
end

The while-loop should iterate until there's only 1 TRUE value in TF(:,i,j), so then it check again sum(TF(:,i,j) == 1)>1 , returns FALSE and continue to the next iteration of the nested 2-for-loops.

But, as I said, this is my idea and I can't be sure without an example of your matrix. By the way, probably there are better ways to get the most extreme change rather than using nested for-loops.

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