簡體   English   中英

使用for循環在Matlab中進行矩陣乘法

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

我必須乘以D=[cos(pi/18) -sin(pi/18); sin(pi/18) cos(pi/18)] 使用for循環將D=[cos(pi/18) -sin(pi/18); sin(pi/18) cos(pi/18)]乘以X= [0.80;0] 9次。 我想將結果存儲在以下表格中: X=zeros(2,10)

我有點迷路了。

安德斯

您的問題不是很清楚。 循環D*X2x1矩陣) 9次不會得到2x10矩陣。

這是您要找的東西嗎?

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

輸出:

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

注意 :矩陣操作大多數時候不需要在Matlab中循環

如果@jkshah的答案是您想要的結果,請使用他的答案(也許將O矩陣更改為O = ones(1,10) )。 MatLab之所以這樣命名是因為它(或:p)對矩陣運算非常有用,如果可以的話,您最好避免循環。 如果您要使用循環,這是一種方法:

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