How do I store and multiply new matrix value in loop

In each for/while loop there will be a different value of R (depending on value of i and j)
and in each loop the current R will multiply with new R and new R will become current R in next loop. this condition will be over when it meet the stop criteria.
my problem is, i do not know how to store new value and multiply with next value..
clear all
clc
n=9
j=n
theta=0.9
for i=1:(n-1)
while j>-1
R=eye(n,n);
R(i,i)=cos(theta)
R(i,j)=-sin(theta)
R(j,i)=sin(theta)
R(j,j)=cos(theta)
j=j-1
if (j-i<=0)
j=-1;
end
end
i=i+1;
j=n;
end

Answers (1)

mohd - you can use two variables to keep track of R from the previous iteration and the one for the current iteration. The following example illustrates this
R = eye(2,2);
theta = pi/4;
for k=2:10
% initialize R for this iteration
Rk = [cos(theta) -sin(theta); sin(theta) cos(theta)];
R = Rk*R;
end
The above is very simple especially since Rk doesn't change on any iteration of the for loop but the idea can be extended to your problem. Try the above and see what happens!

Categories

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

Tags

Asked:

on 15 Oct 2015

Answered:

on 17 Oct 2015

Community Treasure Hunt

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

Start Hunting!