필터 지우기
필터 지우기

How can I store my results from each iteration of a FOR loop?

조회 수: 3 (최근 30일)
Franco
Franco 2011년 2월 2일
How can you specify the introduction of a new variable for every iteration of a for loop. For instance, is
phi = [a,b,c,d]
for i = 1:3
y = [sin(phi(i)),cos(phi(i));sin(phi(i)),cos(phi(i))]
end
I would like the for loop to specify the new variable for each iteration of the for loop, such that
L = [sin(phi(1)),cos(phi(1));sin(phi(1)),cos(phi(1))]
M =[sin(phi(2)),cos(phi(2));sin(phi(2)),cos(phi(2))]
N =[sin(phi(3)),cos(phi(3));sin(phi(3)),cos(phi(3))]

채택된 답변

Oleg Komarov
Oleg Komarov 2011년 2월 2일
On the other side you could create a [2 x 2 x i] array, i.e. each slice (along the third dimension) is a 2 by 2 matrix evaluated at the i-th value of phi:
% Create phi as a 3d vector (depth vector)
phi(1,1,:) = 1:2;
% Create y
y = [sin(phi),cos(phi);sin(phi),cos(phi)]
y(:,:,1) =
0.8415 0.5403
0.8415 0.5403
y(:,:,2) =
0.9093 -0.4161
0.9093 -0.4161
Oleg

추가 답변 (1개)

Doug Hull
Doug Hull 2011년 2월 2일
This is a bad idea.

카테고리

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