More efficient "for i in array" loops

조회 수: 6 (최근 30일)
Conrad Brindle
Conrad Brindle 2020년 2월 21일
답변: Alex Mcaulley 2020년 2월 21일
I Have a series of equations that need a range of input values, currently I am using a for loop like so.
Is there a more efficient way than having to write a for loop for each equaiton, but use the values from the array?
In the example cos_theta might be needed in mutliple equations later on. The full script has 10 or so loops that are just a bit messy
x = [1 2 3 4 5 6]
c = 1
for i = x
cos_theta = [cos_theta, (1-((2*i)/c))];
end
for i = cos_theta
theta = [theta, acos(i)];
end

답변 (1개)

Alex Mcaulley
Alex Mcaulley 2020년 2월 21일
Using your equations directly:
x = [1 2 3 4 5 6];
c = 1;
cos_theta = (1-((2*x)/c));
theta = acos(cos_theta);

카테고리

Help CenterFile Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by