Remove a specific number of values prior and before a value based on a condition from the other column
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Star Strider
2022-9-1
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.)
采纳的回答
Star Strider
2022-8-31
编辑:Star Strider
2022-8-31
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.
.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!