Creating a 3D surface plot of an array considering two for loops
1 view (last 30 days)
Show older comments
I am trying to input an image in uint8 format and then surface plot results of the intensities of the image. For 1 loop, a multiplier is used to cycle through different sizes of the total sum of the data. The second for loop will use a multiplier to run through different values for the total average of the data. Basically, I want a surface plot that plots the multiplier as the x axis, the different size discs as the y axis, and the index of the sum as the z axis.
The image I used is attached.
The error that reads for this code is:
Index in position 2 exceeds array bounds (must not exceed 1).
Error in Lens_Testing_16mm_3D (line 67)
z(i,k) = z(i,k) + 1; % column index
path_info = fullfile(path, '50mm.bmp');
dir_info = dir(path_info);
filename = strcat(path, dir_info.name);
raw_image = imread(filename)';
multiplier = 0.1:0.1:10.0;
nx = length(multiplier);
fractionofdisc = 0.1:1.0;
ny = length(fractionofdisc);
z = zeros(nx,ny);
for i = 1 : nz
y = fractionofdisc(i);
for k = 1 : nx
x = multiplier(k); % have to create a variable for the x axis of plot
n = mean(mean(raw_image))*x;
Mp2 = raw_image > n;
sum_Mp2 = sum(Mp2);
sum_sum_Mp2 = sum(sum_Mp2); % algorithm stops at a fraction of this number
cumsum = 0;
% While loop needed to sweep across the x values until it reaches
% cumsum
while cumsum < y*sum_sum_Mp2
z(i,k) = z(i,k) + 1; % column index
cumsum = cumsum + sum_Mp2(z(i,k));
%disp(z)
end
end
end
figure()
surf (x,y,z)
2 Comments
Michael Soskind
on 6 May 2020
Hi Steven,
Looks like the error has to do with your indexing. i and k are for the y and x values, respectively. This means that since your x and y arrays are not the same length, you are actually exceeding the matrix.
To make the code work, I recommend flipping i and k in your indexing, such as below:
raw_image = imread('50mm.bmp')';
multiplier = 0.1:0.1:10.0;
nx = length(multiplier);
fractionofdisc = 0.1:0.1:1.0;
ny = length(fractionofdisc);
[X,Y] = meshgrid(fractionofdisc, multiplier);
z = zeros(nx,ny);
for i = 1 : ny
y = fractionofdisc(i);
for k = 1 : nx
x = multiplier(k); % have to create a variable for the x axis of plot
n = mean(mean(raw_image))*x;
Mp2 = raw_image > n;
sum_Mp2 = sum(Mp2);
sum_sum_Mp2 = sum(sum_Mp2); % algorithm stops at a fraction of this number
cumsum = 0;
% While loop needed to sweep across the x values until it reaches
% cumsum
while cumsum < y*sum_sum_Mp2
z(k,i) = z(k,i) + 1; % column index
cumsum = cumsum + sum_Mp2(z(k,i));
%disp(z)
end
end
end
figure()
surf (X,Y,z)
Michael
Answers (1)
Cris LaPierre
on 6 May 2020
Edited: Cris LaPierre
on 6 May 2020
The error is because, assuming the rest of your code is correct, you have reversed your indices for assigning to z.
You initialize z with nx rows and ny columns, but when you assign, you use i (which is nz??? Undefined in the code you have shared) and k (which is nx).
Perhaps another issue here is that you have not specified an increment when declaring fractionofdisc, so it will use the default of 1. This means fractionofdisc=0.1, and ny = 1.
Finally, your code for surf is using x and y, which are scalars. These must at least be vectors. Perhaps you mean to use multiplier and fractionofdisc? Also, z must be a matric, so you'll have to fix fractionofdisc or it will only be a vector, and you'll get an error. Because you need to flip i and k, in surf, your x is now fractionofdisc and your y is multiplier.
2 Comments
Cris LaPierre
on 6 May 2020
Edited: Cris LaPierre
on 6 May 2020
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!