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!