How to create a counter to store multiple solutions

16 ビュー (過去 30 日間)
SGer
SGer 2022 年 8 月 31 日
コメント済み: SGer 2022 年 9 月 1 日
Hi there,
Im trying to create a counter for my QW results. At the moment it's only storing the last 2 values. I would like to create an array with 208 rows and 1 column to store all the results. Could someone please assist.

回答 (1 件)

Karim
Karim 2022 年 8 月 31 日
編集済み: Karim 2022 年 9 月 1 日
Since you have a double loop, the easiest method will be to add a variable e.g. counter. Initialize it to zero, and then add 1 to it just before you want to save some data. You can then use that counter as an index.
% initalize output variables (104 rows 2 column)
Mij_out = zeros( 104, 2);
QW_out = zeros( 104, 2);
A = [0 0.25];
B = [0 3.25];
P = -94.2194;
xi = 0.5:0.5:4;
yi = -1:0.5:5;
% initialize the counter
counter = 0;
for i = 1:8
for j = 1:13
Mij = [xi(i) yi(j)];
UMA = A-Mij;
UMB = B-Mij;
Mag_UMA = sqrt(sum(UMA.^2,2));
Mag_UMB = sqrt(sum(UMB.^2,2));
FAM = UMA ./ Mag_UMA;
FBM = UMB ./ Mag_UMB;
G = [FAM' FBM'];
k = [0;-P];
if rank(G) == rank([G k])
% solve for M2
QW = G\k;
end
% add to the counter
counter = counter + 1;
% save the data
Mij_out( counter,:) = Mij;
QW_out( counter,:) = abs(QW);
end
end
Mij_out
Mij_out = 104×2
0.5000 -1.0000 0.5000 -0.5000 0.5000 0 0.5000 0.5000 0.5000 1.0000 0.5000 1.5000 0.5000 2.0000 0.5000 2.5000 0.5000 3.0000 0.5000 3.5000
QW_out
QW_out = 104×2
42.2822 134.3980 28.3094 118.8165 17.5567 103.2719 17.5567 87.7837 28.3094 72.3883 42.2822 57.1606 57.1606 42.2822 72.3883 28.3094 87.7837 17.5567 103.2719 17.5567
  3 件のコメント
Karim
Karim 2022 年 9 月 1 日
I updated the answer, and included the code from your picture. The code runs without error
SGer
SGer 2022 年 9 月 1 日
Thank you!!

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

カテゴリ

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

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by