简体   繁体   中英

For loop for specific range of numbers in Matlab

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM