How to store the results from a for loop into a matrix?

3 ビュー (過去 30 日間)
John Stahura
John Stahura 2019 年 9 月 20 日
編集済み: madhan ravi 2019 年 9 月 20 日
clear all
clc
F = 2;
x = [.1:.05:1.9];
for i = x;
theta1 = atan((2-i)/1);
theta2 = atan(1/(2-i));
theta3 = atan(1/i);
A = [-1 -cos(theta2) 0 0 0 0 0 0 0 0; %Fab
0 sin(theta2) 0 0 0 0 0 0 0 0; %Fac
1 0 -1 0 0 0 0 0 0 0; %Fbd
0 0 0 1 0 0 0 0 0 0; %Fbc
0 sin(theta1) 0 0 -1 -cos(theta3) 0 0 0 0; %Fce
0 -cos(theta1) 0 -1 0 -sin(theta3) 0 0 0 0; %Fcd
0 0 1 0 0 cos(theta3) -1 0 0 0; %Dx
0 0 0 0 0 sin(theta3) 0 1 0 0; %Fde
0 0 0 0 1 0 0 0 -1 0; %Ex
0 0 0 0 0 0 0 -1 0 1]; %Ey
b = [0 F 0 F/2 0 0 0 0 0 0]';
Reactions = A\b
end
How do i store the "Reactions" results from each iteration of the loop into one matrix?

採用された回答

thoughtGarden
thoughtGarden 2019 年 9 月 20 日
編集済み: thoughtGarden 2019 年 9 月 20 日
Note that there area some poor practices in place here, but with the most minimal change to what you already have is to create a blank matrix Reactions and then add each column to that matrix as they are developed.
clear all
clc
F = 2;
x = [.1:.05:1.9];
Reactions = [];
for i = x;
theta1 = atan((2-i)/1);
theta2 = atan(1/(2-i));
theta3 = atan(1/i);
A = [-1 -cos(theta2) 0 0 0 0 0 0 0 0; %Fab
0 sin(theta2) 0 0 0 0 0 0 0 0; %Fac
1 0 -1 0 0 0 0 0 0 0; %Fbd
0 0 0 1 0 0 0 0 0 0; %Fbc
0 sin(theta1) 0 0 -1 -cos(theta3) 0 0 0 0; %Fce
0 -cos(theta1) 0 -1 0 -sin(theta3) 0 0 0 0; %Fcd
0 0 1 0 0 cos(theta3) -1 0 0 0; %Dx
0 0 0 0 0 sin(theta3) 0 1 0 0; %Fde
0 0 0 0 1 0 0 0 -1 0; %Ex
0 0 0 0 0 0 0 -1 0 1]; %Ey
b = [0 F 0 F/2 0 0 0 0 0 0]';
Reactions = [Reactions, A\b];
end
a better solution would be to loop over index value (instead of the values of x) and preallocate the matrix.
clear all
clc
F = 2;
x = [.1:.05:1.9];
Reactions = zeros(10,size(x,1));
for ii = 1:length(x)
theta1 = atan((2-x(ii))/1);
theta2 = atan(1/(2-x(ii)));
theta3 = atan(1/x(ii));
A = [-1 -cos(theta2) 0 0 0 0 0 0 0 0; %Fab
0 sin(theta2) 0 0 0 0 0 0 0 0; %Fac
1 0 -1 0 0 0 0 0 0 0; %Fbd
0 0 0 1 0 0 0 0 0 0; %Fbc
0 sin(theta1) 0 0 -1 -cos(theta3) 0 0 0 0; %Fce
0 -cos(theta1) 0 -1 0 -sin(theta3) 0 0 0 0; %Fcd
0 0 1 0 0 cos(theta3) -1 0 0 0; %Dx
0 0 0 0 0 sin(theta3) 0 1 0 0; %Fde
0 0 0 0 1 0 0 0 -1 0; %Ex
0 0 0 0 0 0 0 -1 0 1]; %Ey
b = [0 F 0 F/2 0 0 0 0 0 0]';
Reactions(:,ii) = A\b;
end
disp(Reactions)
Both are valid, but the later is more efficient and clear to future readers.
  1 件のコメント
madhan ravi
madhan ravi 2019 年 9 月 20 日
編集済み: madhan ravi 2019 年 9 月 20 日
Preallocation is preferred over appending datas.

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

その他の回答 (1 件)

madhan ravi
madhan ravi 2019 年 9 月 20 日
編集済み: madhan ravi 2019 年 9 月 20 日
F = 2;
x = .1:.05:1.9;
Reactions = cell(numel(x),1);
b = [0 F 0 F/2 0 0 0 0 0 0]'; % there's no point in defining it over and over again inside the loop.
for ii = 1:numel(x);
theta1 = atan((2-x(ii))/1);
theta2 = atan(1/(2-x(ii)));
theta3 = atan(1/x(ii));
A = [-1 -cos(theta2) 0 0 0 0 0 0 0 0; %Fab
0 sin(theta2) 0 0 0 0 0 0 0 0; %Fac
1 0 -1 0 0 0 0 0 0 0; %Fbd
0 0 0 1 0 0 0 0 0 0; %Fbc
0 sin(theta1) 0 0 -1 -cos(theta3) 0 0 0 0; %Fce
0 -cos(theta1) 0 -1 0 -sin(theta3) 0 0 0 0; %Fcd
0 0 1 0 0 cos(theta3) -1 0 0 0; %Dx
0 0 0 0 0 sin(theta3) 0 1 0 0; %Fde
0 0 0 0 1 0 0 0 -1 0; %Ex
0 0 0 0 0 0 0 -1 0 1]; %Ey
Reactions{ii} = A\b;
end
celldisp(Reactions)
cat(2,Reactions{:}) % if you want a matrix

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by