※ 引述《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