필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Steven Manz
Steven Manz 2020년 5월 6일
편집: Cris LaPierre 2020년 5월 6일
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
Michael Soskind
Michael Soskind 2020년 5월 6일
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 2020년 5월 6일
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?

댓글을 달려면 로그인하십시오.

답변 (1개)

Cris LaPierre
Cris LaPierre 2020년 5월 6일
편집: Cris LaPierre 2020년 5월 6일
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
Steven Manz
Steven Manz 2020년 5월 6일
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 2020년 5월 6일
편집: Cris LaPierre 2020년 5월 6일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by