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:
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 个评论
Anastasiia Khibovska
Thank you again! Do you by any chance know a good article or tutorial on the composition of the struct and how to access things like column vectors? I am migrating from python/Julia to matlab and find to be a bit more confusing for me! :)
Star Strider
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
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]
data = struct with fields:
Value: [2224 23 2243 33 44 1111 222 4444 5555 23] Condition: [0 0 0 0 1 1 0 0 0 0]
idx = find(data(i).Condition) + [-2 1]
idx = 1×2
3 7
data(i).Value(idx(1):idx(2)) = []
data = struct with fields:
Value: [2224 23 4444 5555 23] Condition: [0 0 0 0 1 1 0 0 0 0]
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.
.
  1 个评论
Anastasiia Khibovska
Thank you for your answer! I am able to do the following code, now I am trying to adapt it to Struct:
where for each trial, I have indexes for the condition under blinkid
however, when I apply the loop to each sample in the struct you have thankfully provided, it throws me an error "Index in position 1 exceeds array bounds."
Would you know the cause for this error?
Thabk you so much for your help!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by