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