1

I want to do a for loop in matlab for a specific numbers only. my problem is I want them to return as 5 different 3x3 matrices but my code only returns at one matrix 15x3. here is my code:

for a = [0;10;20;30;45]

   T = [ cosd(a).^2 sind(a).^2   -sind(2*a);
       sind(a).^2   cosd(a).^2   sind(2*a);
       .5*sind(2*a)    -.5*sind(2*a)   cosd(2*a)];
end

Thank You

1 Answer 1

1

The mistake you're doing is that your code doesn't take each value of a separately. It takes it as a vector and your for loop does not do anything here. Here is how it can be fixed:

a = [0;10;20;30;45];

T = zeros(3,3,5);    %Pre-allocation
for k=1:numel(a)  
   T(:,:,k) = [  cosd(a(k)).^2       sind(a(k)).^2    -sind(2*a(k));
                 sind(a(k)).^2       cosd(a(k)).^2     sind(2*a(k));
               .5*sind(2*a(k))    -.5*sind(2*a(k))     cosd(2*a(k)) ];
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.