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!
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
Related
I am writing a Matlab Tool and certain processes have to be automated.
I am running a for loop, in which some decisions need to be made. Here is a piece of my code:
DecisionMatrix = [0.2 0.4; 0.5 0.7];
Beta =0:pi/20:pi;
Span_Loc = 0.5*(1-cos(Beta))';
for i=1:length(Span_Loc)
Position = Span(i)
% Check Clean of High Lift
if Position >= DecisionMatrix(1,1) && Position <= DecisionMatrix(1,2)
% HighLift run code here
elseif Position >= DecisionMatrix(2,1) && Position <= DecisionMatrix(2,2)
else
% Clean run code here
end
end
Herein, DecisionMatrix is a variable size matrix which is nx2 always. What I want to do is to determine when the value of Position is between the entries of any row of DecisionMatrix. This should be easy when DecisionMatrix is a constant matrix (as shown above). However, this matrix has a variable number of rows.
Hence, how would you do this?
Thanks in advance!!
To determine when the value of Position (scalar) is between the entries of any row of DecisionMatrix (2-column matrix):
result = any(Position>=DecisionMatrix(:,1) & Position<=DecisionMatrix(:,2));
The above gives a logical result (true or false). If you need to know the indices of the rows that fulfill the condition:
result = find(Position>=DecisionMatrix(:,1) & Position<=DecisionMatrix(:,2));
You can fix your code by introducing another loop and coming out of it when you find the required row.
DecisionMatrix = [0.2 0.4; 0.5 0.7];
Beta =0:pi/20:pi;
Span_Loc = 0.5*(1-cos(Beta))';
for p=1:length(Span_Loc)
Position = Span(p);
for q=1:n
if Position >= DecisionMatrix(q,1) && Position <= DecisionMatrix(q,2)
%do what you want when the condition is true
break
end
end
end
I've run an experiment where a machine exerts a force on a bridge until it breaks. I need to cycle through my entire data set and calculate the toughness until I've hit the breaking point (fdValue1).
The toughness is calculated by summing up all the rectangles under my load vs. distance curve. (Basically the integral)
However, I have not been able to find a reasonable way of doing so and my current loop is an infinite loop and I'm not sure why.
%Initializing variables
bridge1Data = xlsread('Bridge1Data.xlsx', 'A2:C2971');
bridge2Data = xlsread('Bridge2Data.xlsx', 'A2:C1440');
bridge1Load = bridge1Data(:, 2);
bridge2Load = bridge2Data(:, 2);
bridge1Dist = bridge1Data(:, 3);
bridge2Dist = bridge2Data(:, 3);
[row1, col1] = size(bridge1Dist);
[row2, col2] = size(bridge2Dist);
bridge1Disp = zeros(row1, col1);
bridge2Disp = zeros(row2, col2);
fdValue1 = 0.000407350000000029;
&Main code
%Displacement
for k = 2:length(bridge1Dist)
bridge1Disp(k-1, 1) = bridge1Dist(k, 1) - bridge1Dist(k-1, 1);
end
%Max Load Bridge 1
maxLoad1 = 0;
for n = 1:length(bridge1Load)
for k = 1
if bridge1Load(n, k) > maxLoad1
maxLoad1 = bridge1Load(n, k);
end
end
end
%Cycle through data till failure, change proj data
totalRect1 = 0;
for j = 2:length(bridge1Disp)
while bridge1Disp(j, 1) ~= fdValue1
rectangle = (bridge1Disp(j, 1) - bridge1Disp(j-1, 1))*...
((bridge1Load(j, 1) + bridge1Load(j-1, 1))/2);
totalRect1 = totalRect1 + rectangle;
end
end
Basically, I make an array for the load and distance the machine pushes down on the bridge, assign a 'Failure Distance' value (fdValue) which should be used to determine when we stop calculating toughness. I then calculate displacement, calculate the maximum load. Then through the variable 'rectangle', calculate each rectangle and sum them all up in 'totalRect1', and use that to calculate the toughness by finding the area under the curve. Is anyone able to see why the loop is an infinite loop?
Thanks
The problem with the condition while bridge1Disp(j, 1) ~= fdValue1 is that you need to check for <= and not for (in)equality, since double numbers will almost never evaluate to be equal even if they seem so. To read more about this you can look here and also google for matlab double comparison. Generally it has something to do with precision issues.
Usually when checking for double equality you should use something like if abs(val-TARGET)<1E-4, and specify some tolerance which you are willing to permit.
Regardless,
You don't need to use loops for what you're trying to do. I'm guessing this comes from some C-programming habits, which are not required in MATLAB.
The 1st loop (Displacement), which computes the difference between every two adjacent elements can be replaced by the function diff like so:
bridge1Disp = diff(bridge1Dist);
The 2nd loop construct (Max Load Bridge 1), which retrieves the maximum element of bridge1Load can be replaced by the command max as follows:
maxLoad1 = max(bridge1Load);
For the last loop construct (Cycle ...) consider the functions I've mentioned above, and also find.
In the code section
%Cycle through data till failure, change proj data
totalRect1 = 0;
for j = 2:length(bridge1Disp)
while bridge1Disp(j, 1) ~= fdValue1
rectangle = (bridge1Disp(j, 1) - bridge1Disp(j-1, 1))*...
((bridge1Load(j, 1) + bridge1Load(j-1, 1))/2);
totalRect1 = totalRect1 + rectangle;
end
end
the test condition of the while loop is
bridge1Disp(j, 1) ~= fdValue1
nevertheless, in the while loop the value of bridge1Disp(j, 1) does not change so if at the first iteratiion of the while loop bridge1Disp(j, 1) is ~= fdValue1 the loop will never end.
Hope this helps.
I am Beginner in Matlab, i would like to plot system concentration vs time plot at a certain time interval following is the code that i have written
%Input function of 9 samples with activity and time calibrated with Well
%counter value approx : 1.856 from all 9 input values of 3 patients
function c_o = Sample_function(td,t_max,A,B)
t =(0 : 100 :5000); % time of the sample post injection in mins
c =(0 : 2275.3 :113765);
A_max= max(c); %Max value of Concentration (Peak of the curve)
if (t >=0 && t <= td)
c_o(t)=0;
else if(td <=t && t<=t_max)
c_o(t)= A_max*(t-td);
else if(t >= t_max)
c_o(t)=(A(1)*exp(-B(1)*(t-t_max)))+(A(2)*exp(-B(2)*(t- t_max)))+...
(A(3)*exp(-B(3)*(t-t_max)));
end
fprintf('plotting Data ...\n');
hold on;
figure;
plot(c_o);
xlabel('Activity of the sample Ba/ml ');
ylabel('time of the sample in minutes');
title (' Input function: Activity sample VS time ');
pause;
end
I am getting following error
Operands to the || and && operators must be convertible to logical scalar values.
Error in Sample_function (line 18)
if (t >=0 && t <= td)
Kindly .Let me know if my logic is incorrect
Your t is not a single value to compare with 0 so it cannot evaluate to true or false.
You want to do this with logical indexing
c_o = zeros(size(t));
c_o(t>=0 & t<=td) = 0; % this line is actually redundant and unnecessary since we initialized the vector to zeros
c_o(t>td & t<=t_max) = A_max*(t(t>td & t<=t_max)-td);
c_o(t>t_max) = (A(1)*exp(-B(1)*(t(t>t_max)-t_max)))+(A(2)*exp(-B(2)*(t(t>t_max)- t_max)))...
+ (A(3)*exp(-B(3)*(t(t>t_max)-t_max)));
You could also make this a little prettier (and easier to read) by assigning the logical indexes to variables:
reg1 = (t>=0 & t<=td);
reg2 = (t>td & t<=t_max);
reg3 = (t>t_max);
Then, for instance, the second assignment becomes the much more readable:
c_o(reg2) = A_max*(t(reg2)-td);
t is written as a array of numbers. So, it can't be compared with a scalar value ex. 0.
Try it in a for loop
for i=1:length(t)
if (t(i) >=0 && t(i) <= td)
c_o(t(i))=0;
else if(td <=t(i) && t(i)<=t_max)
c_o(t(i)))= A_max*(t(i)-td);
else if(t(i) >= t_max)
c_o(t)=(A(1)*exp(-B(1)*(t(i)-t_max)))+(A(2)*exp(-B(2)*(t(i)- t_max)))...
+ (A(3)*exp(-B(3)*(t(i)-t_max)));
end
end
I am trying to create a function in MATLAB:
It simulates a random walk and I want to count the number of steps it took to reach a boundary point as specified by user from origin (0,0)
they will specify: (b,z)
b: is the x point on the boundary it reaches in conjunction with
z: as the y point on the boundary.
Currently it is stopping after it reaches either the b or z value, but I want both to be reached. for example: I want it to reach (3,3) but it will stop running after it reaches (3,1) thus only satisfying one of my constraints.
Any help will be greatly appreciated!
Here is my code:
function s= rw_selectpoint(b,z)
%%exit time for square
%%set up square 2bx2z
%%center of square =(0,0)
x=0;
y=0;
walk_num=0;
%%set up probabilities and loop
while (abs(x)< b && abs(y)<z)
r= rand();
if (r<=.25)
walk_num=walk_num+1;
x=x+1
elseif ((.25<r) && (r<=.5))
walk_num=walk_num+1;
x=x-1
elseif ((.5<r) && (r<=.75))
walk_num=walk_num+1;
y=y+1
else %(.75<r)
walk_num=walk_num+1;
y=y-1
end
end
s=walk_num;
display (x)
display (y)
%display (s)
end
The condition for your while loop is a conjunction of two statements.
while (abs(x)< b && abs(y)<z)
Thus, if one of them is false, then the condition is not satisfied and the loop terminates.
Change the && to || and your code should work properly.
I have done a MATLAB version of the push-relabel FIFO code (exactly like the one on wikipedia and tried it. The discharge function is exactly like wikipedia.
It works perfectly for small graphs (e.g. number of Nodes = 7). However, when I increase my graph size (i.e. number of nodes/vertices > 3500 or more) the "relabel" function runs very slowly, which is called in the "discharge" function. My graphs are huge (i.e. > 3000nodes) so I need to optimize my code.
I tried to optimize the code according to WIKIPEDIA suggestions for global relabeling/gap relabeling:
1) Make neighbour lists for each node, and let the index seen[u] be an iterator over this, instead of the range .
2) Use a gap heuristic.
I'm stuck at the first one , I don't understand what exactly I have to do since it seems there's details left out. (I made neighbor lists such that for vertex u, any connected nodes v(1..n) to u is in the neighbor list already, just not sure how to iterate with the seen[u] index).
[r,c] = find(C);
uc = unique(c);
s = struct;
for i=1:length(uc)
ind = find(c == uc(i));
s(uc(i)).n = [r(ind)];
end
AND the discharge function uses the 's' neighborhood struct list:
while (excess(u) > 0) %test if excess of current node is >0, if so...
if (seen(u) <= length(s(u).n)) %check next neighbor
v = s(u).n(seen(u));
resC = C(u,v) - F(u,v);
if ((resC > 0) && (height(u) == height(v) + 1)) %and if not all neighbours have been tried since last relabel
[C, F, excess] = push(C, F, excess, u, v); %push into untried neighbour
else
seen(u) = seen(u) + 1;
height = relabel(C, F, height, u, N);
end
else
height = relabel(C, F, height, u, N);
seen(u) = 1; %relabel start of queue
end
end
Can someone direct, show or help me please?