Creating a 3D surface plot of an array considering two for loops

8 visualizaciones (últimos 30 días)
Steven Manz
Steven Manz el 6 de Mayo de 2020
Editada: Cris LaPierre el 6 de Mayo de 2020
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 comentarios
Michael Soskind
Michael Soskind el 6 de Mayo de 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
Steven Manz
Steven Manz el 6 de Mayo de 2020
That was it! I just needed to change my x and y to match the number of indexing. Is there a way I can implement an if statement to always check to make sure that these are the same and stop the code if not?

Iniciar sesión para comentar.

Respuestas (1)

Cris LaPierre
Cris LaPierre el 6 de Mayo de 2020
Editada: Cris LaPierre el 6 de Mayo de 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 comentarios
Steven Manz
Steven Manz el 6 de Mayo de 2020
I made the typo that you are stating when typing the code on here. I am sorry for the confusion. Hoowever, yes, you are correct. I fixed the code based on Micheal's answer and fixed my indices and I received what I wanted.
Is there a way I can implement an 'if' statement to always check to make sure that the indices for x and y are the same and stop the code if not?
Cris LaPierre
Cris LaPierre el 6 de Mayo de 2020
Editada: Cris LaPierre el 6 de Mayo de 2020
Ah, typos can kill you in coding. I recommend using copy/paste.
Is it possible to use an if-else statement? Sure. You could also throw a warning message back. If you need to stop execution of the entire program, you could also consider using the error function if they are not equal.
If you need help implementing an if statement, consider going through chapter 13 of MATLAB Onramp.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by