Running code for different variable values.

2 views (last 30 days)
Lets say I have these values here.
theta = 0:2:12.
How do I get matlab to go through the code while the value is zero, get the data, then go through the code while the value is 2, and so on and so forth.
I have been using the for loop...
for i = 1:length(theta)
code code code
code code code
end
but it seems to run a line for all values, save that in an array, then do it for the next line. As a result im running into array incompatibility issues, because my code has 1 by 476 matrix inside of it as well. So I just want to know how yall would go through this.
For example, phi is a 1 by 476 matrix, so when I get to
alpha = theta - phi,
im getting an imcompatible size error. But I want theta to equal zero for the whole code, then I want matlab to come back and solve it while theta is 2, in a clean way.

Accepted Answer

Walter Roberson
Walter Roberson on 15 Mar 2024
theta = 0:2:12;
num_theta = length(theta);
alpha = zeros(num_theta, 147);
for i = 1 : num_theta
this_theta = theta(i);
code code code
alpha(i, :) = this_theta - phi;
end

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!