简体   繁体   English

使用for循环在Matlab中进行矩阵乘法

[英]matrix multiplication in matlab, using a for loop

I have to multiply D=[cos(pi/18) -sin(pi/18); sin(pi/18) cos(pi/18)] 我必须乘以D=[cos(pi/18) -sin(pi/18); sin(pi/18) cos(pi/18)] D=[cos(pi/18) -sin(pi/18); sin(pi/18) cos(pi/18)] by X= [0.80;0] 9 times, using a for loop. 使用for循环将D=[cos(pi/18) -sin(pi/18); sin(pi/18) cos(pi/18)]乘以X= [0.80;0] 9次。 I want to store the results in a table of the form: X=zeros(2,10) 我想将结果存储在以下表格中: X=zeros(2,10)

I'm a bit lost. 我有点迷路了。

Anders. 安德斯

Your question is not very clear. 您的问题不是很清楚。 Looping D*X ( 2x1 matrix) 9 times doesn't give you 2x10 matrix. 循环D*X2x1矩阵) 9次不会得到2x10矩阵。

Is this what you're looking for? 这是您要找的东西吗?

D = [cos(pi/18) -sin(pi/18); sin(pi/18) cos(pi/18)];
X = [0.80;0];
O = ones(1,9);
A = D*X*O

Output: 输出:

A =

 Columns 1 through 8:

   0.78785   0.78785   0.78785   0.78785   0.78785   0.78785   0.78785   0.78785
   0.13892   0.13892   0.13892   0.13892   0.13892   0.13892   0.13892   0.13892

 Column 9:

   0.78785
   0.13892

Note : Matrix manipulations don't need loop in Matlab most of the times 注意 :矩阵操作大多数时候不需要在Matlab中循环

IF the result of @jkshah's answer is what you want, use his answer (maybe change the O matrix to O = ones(1,10) ). 如果@jkshah的答案是您想要的结果,请使用他的答案(也许将O矩阵更改为O = ones(1,10) )。 The reason MatLab is named like this is because it (or she :p ) is great with matrix operations and if you can you better avoid loops. MatLab之所以这样命名是因为它(或:p)对矩阵运算非常有用,如果可以的话,您最好避免循环。 In case you want to work with loops this is a way to do it: 如果您要使用循环,这是一种方法:

D = [cos(pi/18) -sin(pi/18); sin(pi/18) cos(pi/18)]; % Input
X = [0.80;0];                                        % Input
A = D*X;                                             % The function
X = zeros(2,10);                                     % Initialize the result table

X(:,1)=A;                                            % Insert the result in 
                                                     % the first column

for i =1:9                                           % Iterate to fill the 2X10 table
    X(:,i+1) = A;
end 

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

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