Truly excellent. It has everything you could ask of a Matlab program: good structure, excellent comments, simplicity, flow and efficiency. Thank you for sharing.

It is true that there are different ways of going about this problem, one of which is using the unique function. However, the whole idea of developing the nc program was to avoid the "unique" function as it is slower. Note that the speed of a function is more accurately measured via the profiler program of Matlab, or through the cputime function.

21 Jan 2010

spheresegmentvolume
Volume of a (hyper)sphere, sphere cap, or any band through a sphere defined by two parallel planes
Author: John D'Errico

An excellent piece of work. I particularly liked the clear line of thought through the code (mirrored in its simplicity), as well as the abundance of comments.
Thank you for sharing.

Comments and Ratings on Zacharias Voulgaris' Files

Actually, the bug was for any power of 2 (1, 2, 4, ...). Ali's modification works, but a more elegant fix is:
c = floor(log2(x)) + 1;
No need for any other modification from original code (no special case for 1, no need to remove preceding zeros).

% Convert a decimanl number into a binary array
%
% Similar to dec2bin but yields a numerical array instead of a string and is found to
% be rather faster

if x==1
y=1;
return
end

c = ceil(log(x)/log(2)) + 1; % Number of divisions necessary ( rounding up the log2(x) )
y(c) = 0; % Initialize output array
for i = 1:c
r = floor(x / 2);
y(c+1-i) = x - 2*r;
x = r;
end

% If there is a preceding one, remove it.
if(y(1) == 0)
y(1) = [];
end

Brilliant! if anyone is looking for a FAST decimal to binary / binary to decimal converter, this is the one - please don't use bi2de or de2bi.
This tremendously increased my performance,
thank you
Zacharias Voulgaris

Actually, the bug was for any power of 2 (1, 2, 4, ...). Ali's modification works, but a more elegant fix is:
c = floor(log2(x)) + 1;
No need for any other modification from original code (no special case for 1, no need to remove preceding zeros).

More correct version:
function y = d2b(x)
% Convert a decimanl number into a binary array
%
% Similar to dec2bin but yields a numerical array instead of a string and is found to
% be rather faster
if x==1
y=1;
return
end
c = ceil(log(x)/log(2)) + 1; % Number of divisions necessary ( rounding up the log2(x) )
y(c) = 0; % Initialize output array
for i = 1:c
r = floor(x / 2);
y(c+1-i) = x - 2*r;
x = r;
end
% If there is a preceding one, remove it.
if(y(1) == 0)
y(1) = [];
end

Comment only