Matlab 当零元素数超过阈值避免循环时,删除矩阵的某些列
我有一个相当大的Matlab 当零元素数超过阈值避免循环时,删除矩阵的某些列,matlab,vectorization,Matlab,Vectorization,我有一个相当大的(107 x n)矩阵x。在这些n列中,每个三列属于彼此。因此,矩阵X的前三列构建一个块,然后是列4,5,6,依此类推 在每个块中,first列的first 100行元素非常重要X(1:100,1:3:end)。当第一列中的零或南的数量大于或等于20时,应删除整个块 有没有一种没有循环的方法 谢谢你的建议 假设输入的列数是3的倍数,这里可能有两种方法 方法#1 %// parameters rl = 100; %// row limit cl = 20; %// count l
(107 x n)
矩阵x
。在这些n
列中,每个三列属于彼此。因此,矩阵X
的前三列构建一个块,然后是列4,5,6
,依此类推
在每个块中,first
列的first 100
行元素非常重要X(1:100,1:3:end)
。当第一列中的零
或南
的数量大于或等于20
时,应删除整个块
有没有一种没有循环的方法
谢谢你的建议 假设输入的列数是3的倍数,这里可能有两种方法
方法#1
%// parameters
rl = 100; %// row limit
cl = 20; %// count limit
X1 = X(1:rl,1:3:end) %// Important elements from input
match_mat = isnan(X1) | X1==0 %// binary array of matches
match_blk_id = find(sum(match_mat)>=cl) %// blocks that satisfy requirements
match_colstart = (match_blk_id-1).*3+1 %// start column indices that satisfy
all_col_ind = bsxfun(@plus,match_colstart,[0:2]') %//'columns indices to be removed
X(:,all_col_ind)=[] %// final output after removing to be removed columns
或者如果你喜欢“紧凑”代码-
方法#2
X1 = X(1:rl,1:3:end)
match_mat = isnan(X1) | X1==0 %// binary array of matches
X(:,repmat(sum(match_mat)>=cl,[3 1]))=[] %// Find matching blocks, replicate to
%// next two columns and remove them from X
注意:如果X
不是3
的倍数,请在使用代码之前使用此选项-X=[X零(大小(X,1),3-mod(大小(X,2,3))]
如果X不是3
的倍数,请用0-X=[X零(大小(X,1),3-mod(大小(X,2),3)]
填充,然后使用此解决方案。非常感谢divakar!乍一看,它似乎解决了问题,至少它正确地截断了X矩阵。现在该由我来理解你是如何解决的:-)p.s.n是three@rookieMI使用一个小的输入,从每个步骤中获得输出并进行分析。这些小评论也一定有帮助!:)@新秀们也来看看吧#2!更加优雅:-)
X1 = X(1:rl,1:3:end)
match_mat = isnan(X1) | X1==0 %// binary array of matches
X(:,repmat(sum(match_mat)>=cl,[3 1]))=[] %// Find matching blocks, replicate to
%// next two columns and remove them from X