data_test是一个由带有一些NaN的数字填充的向量。
data_test = [NaN, 2, 3, 4, NaN,NaN,NaN, 12 ,44, 34, NaN,5,NaN];
我想根据NaN切割data_test
并创建一个包含NaN之间data_set
个片段的单元格数组。
data_cell{1}=[2 3 4];
data_cell{2}=[12 44 34];
data_cell{3}=[5];
此时我需要过滤这些值(这是正常的,仅作为示例,过滤后的值将与data_test +1相同)
data_cell{1} -> data_cell_filt{1}
data_cell{2} -> data_cell_filt{2}
data_cell{3} -> data_cell_filt{3}
并将过滤后的值放回data_test。
data_cell_filt{1}
data_cell_filt{2} -> data_test
data_cell_filt{3}
以使data_test为
data_test = [NaN, 3, 4, 5, NaN,NaN,NaN, 13 ,45, 35, NaN, 6, NaN];
ps(在我的情况下,data_test是~20000个元素)
答案 0 :(得分:3)
您可以使用循环轻松完成或使用arrayfun
这样:
A = [NaN, 2, 3, 4, NaN, NaN, NaN, 13, 45, 35, NaN, 6, NaN]
i1 = find(diff(isnan(A))==-1)+1 %// Index where clusters of numbers begin
i2 = find(diff(isnan(A))==1) %// Index where clusters of numbers end
data_cell_filt = arrayfun(@(x,y)({A(x:y)}),i1,i2 ,'uni', false)
答案 1 :(得分:3)
一个包含accumarray
和cumsum
以及diff
%// find the index of regular numbers
idx = find(~isnan(data_test))
%// group the numbers which are adjacent, to some index number
idx1 = cumsum([1,diff(idx)~=1])
%// put all those numbers of same index number into a cell
out = accumarray(idx1.',data_test(idx).',[],@(x) {x.'})
示例运行
data_test = [NaN, 2, 3, 4, NaN,NaN,NaN, 12 ,44, 34, NaN,5,NaN];
>> celldisp(out)
out{1} =
2 3 4
out{2} =
12 44 34
out{3} =
5
答案 2 :(得分:3)
基于卷积的方法:
ind = isnan(data_test);
t = conv(2*x-1, [-1 1], 'same'); %// convolution is like correlation but flips 2nd input
starts = find(t==2); %// indices of where a run of non-NaN's starts, minus 1
ends = find(t==-2); %// indices of where it ends
result = mat2cell(data_test(~ind), 1, ends-starts); %// pick non-NaN's and split