简体   繁体   English

Matlab中特定数字范围的for循环

[英]For loop for specific range of numbers in Matlab

I want to do a for loop in matlab for a specific numbers only.我只想在 matlab 中为特定数字做一个 for 循环。 my problem is I want them to return as 5 different 3x3 matrices but my code only returns at one matrix 15x3.我的问题是我希望它们作为 5 个不同的 3x3 矩阵返回,但我的代码只返回一个 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.您正在做的错误是您的代码没有单独使用a每个值。 It takes it as a vector and your for loop does not do anything here.它把它当作一个向量,你的for循环在这里不做任何事情。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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