MATLAB:用第二个值对一个数组进行子集化

时间:2018-10-17 21:46:38

标签: matlab

我基本上有两列(数组):A列代表跨时间点的连续数据流(例如,血压上升和下降),而B列代表事件的发生(例如,电击或深呼吸) 。列A具有每个单元格的值,而列B仅具有发生事件的时间点的值,这些值表示不同类型事件(例如5种类型的1、2、3、4、5)的发生的代码。

什么代码可以使用B列中的值子集A列中的数据(例如,收集事件1和2,事件1和3或事件1和4之间的任何时间点的所有数据)?基本上,我试图仅提取某些时间段的值,并将其存储在单元格数组中。

示例:

我所拥有的:

数组A:10、12、13、20、15、16、14、9、8、11、12、15、14

数组B:1、0、2、0、0、0、1、0、2、0、1、0、2

 *(where in Array B, 1 and 2 are events--say a showing a cue and a subject 
 responding to that cue--and I want the data between a 1 and a 2)*

我想要的:

(新)单元格数组C:[12,13],[9,8],[15,14]

 *That is, it's grabbing the data from Array A, based on what falls between 
 1s and 2s in Array B, and storing them into cells of Array C*

非常感谢!

1 个答案:

答案 0 :(得分:0)

这是一种方法:

  1. 找到开始和结束的索引。可以使用strfind完成此操作,该方法也适用于数字数组。
  2. 使用这些索引来循环构建结果。

ind_start = strfind(B, [1 0]); % Or: ind_start = strfind(char(B+48), '10');
ind_end = strfind(B, 2); % Or: ind_end = strfind(char(B+48), '2')
result = cell(size(ind_start));
for k = 1:numel(ind_start)
    result{k} = A(ind_start(k)+1:ind_end(k));
end