フィルターのクリア

I am trying to make a new matrix for each iteration of a for loop

41 ビュー (過去 30 日間)
Kiernan O'Boyle
Kiernan O'Boyle 2022 年 4 月 27 日
コメント済み: Stephen23 2022 年 4 月 27 日
I am not sure how to make a new matrix for each iteration, I would like to have Q_bar1, Q_bar2, Q_bar3, Q_bar4 each having differnt values. I know I need to initalize if I am using Q_bar(i) but I am not sure how to do that.
%% Making Qbar matrices for all thetas
for i = 1:length(theta)
m = cosd(theta(i));
n = sind(theta(i));
T1 = [m^2, n^2, 2*n*m
n^2, m^2, -2*n*m
-n*m, n*m, (m^2)-(n^2)];
T2 = [m^2, n^2, n*m
n^2, m^2, -n*m
-2*n*m, 2*n*m, (m^2)-(n^2)];
Q_bar(i)= inv(T1)*Q*T2;
end
ERROR MESSAGE:
Unable to perform assignment because the indices on the left side are not compatible with the
size of the right side.
Error in project2 (line 85)
Q_bar(i)= inv(T1)*Q*T2;
  1 件のコメント
Stephen23
Stephen23 2022 年 4 月 27 日
You should replace this
inv(T1) * Q * T2
with the more efficient and numerically more robust
T1 \ Q * T2

サインインしてコメントする。

回答 (1 件)

Jan
Jan 2022 年 4 月 27 日
編集済み: Jan 2022 年 4 月 27 日
Q_bar = cell(1, length(theta));
for i = 1:length(theta)
m = cosd(theta(i));
n = sind(theta(i));
T1 = [m^2, n^2, 2*n*m; ...
n^2, m^2, -2*n*m; ...
-n*m, n*m, m^2 - n^2];
T2 = [m^2, n^2, n*m; ...
n^2, m^2, -n*m: ...
-2*n*m, 2*n*m, m^2 - n^2];
Q_bar{i} = inv(T1) * Q * T2;
end
Or alterntively:
Q_bar = zeros(3, 3, length(theta));
for i = 1:length(theta)
m = cosd(theta(i));
n = sind(theta(i));
T1 = [m^2, n^2, 2*n*m; ...
n^2, m^2, -2*n*m; ...
-n*m, n*m, m^2 - n^2];
T2 = [m^2, n^2, n*m; ...
n^2, m^2, -n*m: ...
-2*n*m, 2*n*m, m^2 - n^2];
Q_bar(:, :, i) = inv(T1) * Q * T2;
end
Note, that T1 \ Q * T2 is numerically more stable than calculating the inverse explicitely.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by