要扩展@rayryeng的示例(奖金)答案:function out = filter_python(in, func)
out = in(func(in));
end
这种想法可以扩展到二维数组中的整行数据。在function out = filtrow(func,in,colnum)
if nargin == 2
colnum=0;
sz = size(in);
if sz(1)==1 || sz(2)==1
out = in(func(in));
return
else
out = in;
for i = sz(2):-1:1
out = out(func(out(:,i)),:);
end
end
end
if nargin == 3
out = in(func(in(:,colnum)),:);
end
或者如果你想喝可乐的话。在
^{pr2}$
所以给定矩阵A:A =
0 0.4694 0.1656 0.0838 0.0782 0.8687 0.1818
0.0540 0 0.6020 0.2290 0.4427 0.0844 0.2638
0.5308 0.3371 0 0.9133 0.1067 0.3998 0.1455
0.7792 0.1622 0.6541 0 0.9619 0.2599 0.1361
0.9340 0.7943 0.6892 0.8258 0.0046 0.8001 0.8693
0.1299 0.3112 0.7482 0.5383 0.7749 0.4314 0.5797
0.5688 0.5285 0.4505 0.9961 0.8173 0.9106 0.5499
#%%% We can also remove entire rows or cols by this method
#%%% here we use filtrow to remove rows where elements do no pass the c condition function
>> out = filtrow(@(x) x>0,A)
out =
0.9340 0.7943 0.6892 0.8258 0.0046 0.8001 0.8693
0.1299 0.3112 0.7482 0.5383 0.7749 0.4314 0.5797
0.5688 0.5285 0.4505 0.9961 0.8173 0.9106 0.5499
#%%% Or we can do the same to remove the cols
>> out = filtcol(@(x) x>0,A)
out =
0.0782 0.8687 0.1818
0.4427 0.0844 0.2638
0.1067 0.3998 0.1455
0.9619 0.2599 0.1361
0.0046 0.8001 0.8693
0.7749 0.4314 0.5797
0.8173 0.9106 0.5499
#%%%Or you could filter by individual rows
>> out = filtrow(@(x) x>0,A,1)
out =
0.0540 0 0.6020 0.2290 0.4427 0.0844 0.2638
0.5308 0.3371 0 0.9133 0.1067 0.3998 0.1455
0.7792 0.1622 0.6541 0 0.9619 0.2599 0.1361
0.9340 0.7943 0.6892 0.8258 0.0046 0.8001 0.8693
0.1299 0.3112 0.7482 0.5383 0.7749 0.4314 0.5797
0.5688 0.5285 0.4505 0.9961 0.8173 0.9106 0.5499