Reputation: 396
I am working on a scilab program to average a 3d matrix in cubes.This is mostly done, but I want to set the size to a given sum,(the voxels all add up to a set amount in each cube)and I keep breaking the program every time I try to add this feature.
function totSAR = comptS(Material, SAR, a, b, c, a1, b1, c1, grams)
si=size(SAR);
radius=0;
OK=0;
totwei=0;
totSAR=0;
totpx=0;
while (totwei<=grams*1e-3 && OK==0)
totwei2=totwei;
totSAR2=totSAR;
totpx2=totpx;
totwei=0;
totpx=0;
totSAR1=0;
totSAR=0;
radius=radius+1;
for o=-radius:radius
rado=floor(sqrt(radius^2-o^2));
for m=-rado:rado
radm=floor(sqrt(rado^2-m^2));
for n=-radm:radm
if (a+m >= 1 && a+m <= si(1) && b+n >=1 && b+n <=si(2) && c+o >= 1 && c+o <si(3))
if totwei<=10e-3
totwei=totwei+a1*b1*c1*Material(a+m, b+n, c+o);
if SAR(a+m, b+n, c+o)>0
totpx=totpx+1;
totSAR=totSAR+SAR(a+m, b+n, c+o);
end
end
end
end
end
end
if totSAR==totSAR1
OK=1;
end
end
coefw = (grams*1e-3 - totwei2)/(totwei-totwei2);
totpxs = coefw*(totpx-totpx2);
totSARs = coefw*(totSAR-totSAR2);
totpx;
if totpx>0
totSAR=(totSAR2+totSARs)/(totpx2+totpxs);
end
end
Sorry that I am a bit of a newbie, and thanks for the help!
Upvotes: 0
Views: 120
Reputation: 2567
Your only problem is that you aren't updating the totwei
variable when you create a voxel.You will need to subtract the value of the voxel from the remaining desired weight, and add it to the total weight.
totwei = totwei + voxel
valleft = valleft - voxel
Upvotes: 1