Running code for different variable values.

조회 수: 3 (최근 30일)
Jaevon Stewart
Jaevon Stewart 2024년 3월 14일
답변: Walter Roberson 2024년 3월 15일
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.

채택된 답변

Walter Roberson
Walter Roberson 2024년 3월 15일
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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by