How to save every iteration of for loop and plot
2 次查看(过去 30 天)
显示 更早的评论
I am attempting to find how long it takes to reach a specific spot in an image. This spot will change based on 'fractionofdisc'. The column index 'n(k)' will tell me how many steps it takes to get to that specific point on the image. This code, however, only spits out 1 value for x and n instead of supplying every value as the for loop iterates. I need help understanding why this might be happening. As an update, it does seem as if the code runs one iteration at x = 0.1 and stops. I would like to know what the code is doing from x = 0.1 to x = 1.0.
fractionofdisc = 0.1:0.1:1.0;
y = uint8(double(mean(mean(raw_image)))*5); % changing data type may not be necessary
Mp2 = raw_image > y;
sum_Mp2 = sum(Mp2);
sum_sum_Mp2 = sum(sum_Mp2);
x = zeros(length(fractionofdisc));
n = zeros(length(fractionofdisc));
for k = 1:length(fractionofdisc)
n(k) = 0; % n must be a variable so it holds information for each iteration
cumsum = 0;
% While loop needed to sweep across the x values until it reaches
% cumsum
x(k) = fractionofdisc(k);
while cumsum < x*sum_sum_Mp2
n(k) = n(k) + 1; % column index
cumsum = cumsum + sum_Mp2(n(k));
end
end
figure(2)
plot(x,n,'-g')
xlabel('Fraction of Disc')
ylabel('Column Index')
0 个评论
回答(1 个)
Akihumi
2020-5-7
What is your numerical range of raw_image? If it is between 0 and 1, all elements of Mp2 will be 0, thus the while loop
while cumsum < x*sum_sum_Mp2
n(k) = n(k) + 1; % column index
cumsum = cumsum + sum_Mp2(n(k));
end
will never be executed, since x*sum_sum_Mp2 = 0.
2 个评论
Akihumi
2020-5-8
% This line sets a threshold that has the value of 5 times of the average of all pixel values
y = uint8(double(mean(mean(raw_image)))*5); % changing data type may not be necessary
% This line labels a zero if the pixel doesn't exceed the threshold,
% otherwise labels a one if it exceeds the threshold
% So the output will always be zeros and ones
Mp2 = raw_image > y;
% This line computes the sum of the columns,
% i.e. the number of pixels that exceed the threshold in one column
sum_Mp2 = sum(Mp2);
% This line computes the total number of pixels that exceed the threshold
sum_sum_Mp2 = sum(sum_Mp2);
I'm nor very sure what is the intention of the following for loop and while loop. Do you want to run through the columns and add up the cumsum with corresponding sum_Mp2 until the cumsum is larger than corresponding fractionofdisc*sum_sum_Mp2?
Note that x is actually also a 1x10 vector that is [0.1,0,0,0...,0] in the first iteration of for loop k, and it'll be [0.1,0.2,0,0,0,...,0] in the 2nd iteration. And for now your while loop condition x*sum_sum_Mp2 will output a 1x10 vector that has trailing zeros in the iterations smaller than k==10. Is that what you wanted?
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!