# Iterating an array in a for loop

37 views (last 30 days)
laura byrne on 22 Feb 2021
Edited: KALYAN ACHARJYA on 22 Feb 2021
Hi I want to iterate an array in a for loop but within a while loop if this makes sense. Im trying to set the condition where while the value of diameter is less than that of final diameter, the loop executes code which is stored in an empty array but I'm not sure how to do this. I'm trying to set up the for loop so that it executes until the array is filled ie until the values of diameter reach the value of diameter final? Thanks in advance for your help :-)
focal_length = 50e-02;
m = 1;
diameter = 0.5e-03
diameter_final = 6e-03;
wavelength = 450e-06
diameter_a = {}
wavelength_a = {}
while diameter <= diameter_final
for size(diameter_a{}) < 1:6
theta = atand(diameter/focal_length);
d = (m*wavelength/sin(theta));
spatial_freq = 1/d
diameter_a = diameter + 0.5e-03
wavelength_a = wavelength + 5e-05
plot(diameter_a, spatial_freq,"r,--*")
ylabel("Spatial frequency (lines/mm)")
hold on
plot(wavelength_a,spatial_freq,"k")
end
end

KALYAN ACHARJYA on 22 Feb 2021
Edited: KALYAN ACHARJYA on 22 Feb 2021
You can avoid loop here
focal_length = 50e-02;
m = 1;
diameter=0.5e-03:0.5e-03:6e-03;
%% Create the Wavelength Array with Same dimension as diameter
% Withn Given Intial value and step size
wavelength_int=450e-06;
wavelength_step=5e-05;
wavelength=wavelength_int+(1:length(diameter))*wavelength_step;
%%
theta=atand(diameter/focal_length);
d=(m*wavelength)./sin(theta);
spatial_freq=1./d;
figure, plot(diameter, spatial_freq,'-*r');
ylabel("Spatial frequency (lines/mm)")
figure, plot(wavelength,spatial_freq,'k');
ylabel("Spatial frequency (lines/mm)")
grid on;
axis tight;
Kindly Do Verify
Note: y data are same for both plots, but x data are different. More you ca plots the data in multiple ways. Please refer MATLAB Docs
##### 2 CommentsShowHide 1 older comment
KALYAN ACHARJYA on 22 Feb 2021
Not suitable for the case
Diameter present Value
Wavelength present Value
while to diameter <=some value Check
calculate spatial_freq based on Diameter present Value & Wavelength present Value
Store spatial_freq; % Plot Variable
Update Diameter
Update Wavelength
Store Updated Diameter & Wavelength % Plot variables
end
Here the diameter data is always one step ahead to check the condition, so its vector size is 1 element larger than spatial_freq and wavelength. The wavelength is updated even after the diameter update (although you can enter a break statement after the diameter update assignment to avoid the extra wavelength data).
Hence in the final plot, I have ignored the last elements of diameter and wavelength. More: Exact preallocation of the array data is not possible here, as we have no idea about the number of iterations (Disadvantage).
focal_length = 50e-02;
m = 1;
spatial_freq=[];
wavelength=[];
wavelength(1)=450e-06;
diameter=[];
diameter(1)=0.5e-03;
i=1;
while diameter<=6e-03
theta=atand(diameter(i)/focal_length);
d=(m*wavelength(i))/sin(theta);
spatial_freq(i)=1/d;
diameter(i+1)=diameter(i)+0.5e-03;
wavelength(i+1)=wavelength(i)+5e-05;
i=i+1;
end
figure, plot(diameter(1:end-1),spatial_freq,'-*r');
ylabel("Spatial frequency (lines/mm)")
grid on;
figure, plot(wavelength(1:end-1),spatial_freq,'k');
ylabel("Spatial frequency (lines/mm)")
grid on;

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by