Remove a specific number of values prior and before a value based on a condition from the other column
1 view (last 30 days)
Show older comments
Anastasiia Khibovska
on 31 Aug 2022
Commented: Star Strider
on 1 Sep 2022
Hello everyone,
Suppose I have a matrix with multiple columns delete 12 data points up or after a certain point. The way I select all points in a column Y that are based on a condition from another column X, then I want to delete 12 points that preceed this selection and 12 points after the selection AND the selection itself. I am attaching a picture of what the columns would most ideally looked like after the transformation:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1112465/image.png)
So if I have :
data(i).Value = [2224,23,2243,33,44,1111,222,4444,5555,23]
and
data(i).Condition = [0,0,0,0,1,1,0,0,0,0],
How do I get:
data(i).Value =[2224,23,4444,5555,23]
where the range that equals to 1 in the Condition is deleted AND two values before and after are omitted as well!
3 Comments
Star Strider
on 1 Sep 2022
As always, my pleasure!
See the struct documentation section on Topics under See Also. That is likely the best source of information on what structures are and working with them. I only use them occasionally, so I am not as familiar with them as I am with table arrays, for example. (If presented with ‘data_new’ or ‘data_new(30)’ I would probably use struct2table with it as a personal preference.)
Accepted Answer
Star Strider
on 31 Aug 2022
Edited: Star Strider
on 31 Aug 2022
I am not exactly certain what result you want, since ‘two values before and after are omitted’ seems not to conform with the example posted.
One approach —
i = 1;
data(i).Value = [2224,23,2243,33,44,1111,222,4444,5555,23];
data(i).Condition = [0,0,0,0,1,1,0,0,0,0]
idx = find(data(i).Condition) + [-2 1]
data(i).Value(idx(1):idx(2)) = []
To do this with multiple sets (occurrences) of conditions would likely require a loop to test and eliminate each range of ‘idx’ as well as testing to be certain that the beginning and ends of the vector are considered.
One way to do that could be:
idxrng = max(1,min(idx)-12) : min(numel(data(i).Value),max(idx)+12);
Experiment to get the result you want.
EDIT — Corrected typographical errors.
.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!