作者celestialgod (天)
看板MATLAB
标题Re: [讨论] 列印问题 不用回圈
时间Sun Mar 27 19:40:55 2016
※ 引述《su3cl3 (:))》之铭言:
: 大家好,
: 若我今天要做到以下这件事,但是不使用回圈,有没有任何作法呢?
: B=1000:1000:10000;
: C=1:1000:9001;
: P是一向量(1x1200)
: for i=1:10
: d=length(find(P<B(i)))-length(find(P<C(i))); %d为1x1
: fprintf('%5d-%5d:%5d\n',C(i),B(i),d); % 列印
: end
: 谢谢!
B = 1000:1000:10000;
C = 1:1000:9001;
P = 10000 * rand(1200, 1);
tic
for i=1:10
d=length(find(P<B(i)))-length(find(P<C(i)));
fprintf('%5d-%5d:%5d\n',C(i),B(i),d);
end
toc
% Elapsed time is 0.008919 seconds.
tic
d = arrayfun(@(b, c) sum(P > c & P < b), B, C);
disp(sprintf('%5d-%5d:%5d\n', [C; B; d]));
toc
% Elapsed time is 0.011709 seconds.
tic
d = histcounts(P, 0:1000:10000);
disp(sprintf('%5d-%5d:%5d\n', [C; B; d]));
toc
% Elapsed time is 0.011625 seconds.
PS: histcounts是2014b後才有的函数,用之前的版本请用histc第二个output
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 180.218.152.118
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/MATLAB/M.1459078859.A.CA3.html
※ 编辑: celestialgod (180.218.152.118), 03/27/2016 19:41:48
1F:推 su3cl3: 感谢大大 太厉害了! 03/27 19:50
2F:推 fghjkl1000: 大推! 03/27 20:13