大家好,
我有一個 for 循環,它根據列表中的唯一項目生成單獨的表。 在生成的每個表上,完成計算,並將新列添加到每個表中。 請參閱下面的代碼和我對每一行的註釋。
MaskList = Associative Array(Column(dt3, "mask") << Get Values) << Get Keys; // from dt3, puts unique masks into a list, removes duplicates
show(MaskList); // show for debugging purposes, to be viewed in Log window
// This loop creates a new table for each unique mask in MaskList
For (i = 1, i <= N Items(MaskList), i++,
dt3 << Select Where( :mask == MaskList[i]); // selects the rows of the chosen mask at the i-th position in MaskList
dt4 = dt3 << Subset (Output Table("Mask Table"), Get Selected Rows, "visible"); // outputs a separate table for each unique mask
dt4 << New Column("new_x_offset", Numeric, "Continuous", Format( "Best", 12 ), Formula( Round( :best_x_offset, 2 ) )); // calculation and new column
dt4 << New Column("new_y_offset", Numeric, "Continuous", Format( "Best", 12 ), Formula( Round( :best_y_offset, 2 ) )); // calculation and new column
dt4 << New Column("new_xscl", Numeric, "Continuous", Format( "Best", 12 ), Formula( Round( 1 - (:xscl - (-0.5)), 2 ) )); // calculation and new column
dt4 << New Column("new_yscl", Numeric, "Continuous", Format( "Best", 12 ), Formula( Round( 1 - (:yscl - (-0.5)), 2 ) )); // calculation and new column
dt4 << New Column("new_ort", Numeric, "Continuous", Format( "Best", 12 ), Formula( Round( 1 - (:ort), 2 ) )); // calculation and new column
dt4 << New Column("new_rotg", Numeric, "Continuous", Format( "Best", 12 ), Formula( Round( 1 - (:rotg), 2 ) )); // calculation and new column
dt4 << New Column("new_mag", Numeric, "Continuous", Format( "Best", 12 ), Formula( Round( 1 - (:mag - (-1.0)), 2 ) )); // calculation and new column
dt4 << New Column("new_rotf", Numeric, "Continuous", Format( "Best", 12 ), Formula( Round( 1 - (:rotf - (-1.0)), 2 ) )); // calculation and new column
dt4 << New Column("Mean_x_offset", Numeric, "Continuous", Format( "Best", 12 ), Formula( Col Mean( :new_x_offset ) )); // take average of values in column new_x_offset
dt4 << New Column("Mean_y_offset", Numeric, "Continuous", Format( "Best", 12 ), Formula( Col Mean( :new_y_offset ) )); // take average of values in column new_y_offset
dt4 << New Column("Mean_new_xscl", Numeric, "Continuous", Format( "Best", 12 ), Formula( Col Mean( :new_xscl ) )); // take average of values in column new_xscl
dt4 << New Column("Mean_new_yscl", Numeric, "Continuous", Format( "Best", 12 ), Formula( Col Mean( :new_yscl ) )); // take average of values in column new_yscl
dt4 << New Column("Mean_new_ort", Numeric, "Continuous", Format( "Best", 12 ), Formula( Col Mean( :new_ort ) )); // take average of values in column new_ort
dt4 << New Column("Mean_new_rotg", Numeric, "Continuous", Format( "Best", 12 ), Formula( Col Mean( :new_rotg ) )); // take average of values in column new_rotg
dt4 << New Column("Mean_new_mag", Numeric, "Continuous", Format( "Best", 12 ), Formula( Col Mean( :new_mag ) )); // take average of values in column new_mag
dt4 << New Column("Mean_new_rotf", Numeric, "Continuous", Format( "Best", 12 ), Formula( Col Mean( :new_rotf ) )); // take average of values in column new_rotf
Output_x_offset = Associative Array(Column(dt4, "Mean_x_offset") << Get Values) << Get Keys; // saves average x_offset, removes duplicates
Output_y_offset = Associative Array(Column(dt4, "Mean_y_offset") << Get Values) << Get Keys; // saves average y_offset, removes duplicates
Output_xscl = Associative Array(Column(dt4, "Mean_new_xscl") << Get Values) << Get Keys; // saves average xscl, removes duplicates
Output_yscl = Associative Array(Column(dt4, "Mean_new_yscl") << Get Values) << Get Keys; // saves average yscl, removes duplicates
Output_ort = Associative Array(Column(dt4, "Mean_new_ort") << Get Values) << Get Keys; // saves average ort, removes duplicates
Output_rotg = Associative Array(Column(dt4, "Mean_new_rotg") << Get Values) << Get Keys; // saves average rotg, removes duplicates
Output_mag = Associative Array(Column(dt4, "Mean_new_mag") << Get Values) << Get Keys; // saves average mag, removes duplicates
Output_rotf = Associative Array(Column(dt4, "Mean_new_rotf") << Get Values) << Get Keys; // saves average rotf, removes duplicates
);我的問題有三個:
- 是否可以在不創建新列來存儲數據的情況下取列中值的平均值? 理想情況下,平均值只是一個數字,而不是一列反復出現相同數字的列。 也許只是在新的表、列表或其他東西中吐出,只是使代碼/表更具可讀性並且不那麼臃腫。
這是上述代碼輸出的一部分。 我只是一遍又一遍地獲得具有相同數字的列來計算平均值。

- 對於 for 循環創建的每個數據表,我希望將計算列中的數據存儲在某種表或列表中,以便它們不會被 for 循環的每次新迭代覆蓋。
- 關聯數組函數對列中的數據進行舍入。 可以設置成不這樣做嗎?
我附上了代碼輸出的示例。 為簡單起見,我將 MaskList 限制為 1 項,因此 for 循環僅運行一次。
任何幫助表示讚賞。
謝謝。
我相信下面的腳本將為您提供您想要的單獨數據表以及每個掩碼的方法。它在 dt3 表中創建新的捨入列,然後在舍入列上運行摘要平台(tables=>Summary)。

Names Default To Here( 1 );
dt3 = Data Table( "dt3" );
dt3 << New Column( "new_x_offset", Numeric, "Continuous", Format( "Best", 12 ), Formula( Round( :best_x_offset, 2 ) ) ); // calculation and new column
dt3 << New Column( "new_y_offset", Numeric, "Continuous", Format( "Best", 12 ), Formula( Round( :best_y_offset, 2 ) ) ); // calculation and new column
dt3 << New Column( "new_xscl", Numeric, "Continuous", Format( "Best", 12 ), Formula( Round( 1 - (:xscl - (-0.5)), 2 ) ) ); // calculation and new column
dt3 << New Column( "new_yscl", Numeric, "Continuous", Format( "Best", 12 ), Formula( Round( 1 - (:yscl - (-0.5)), 2 ) ) ); // calculation and new column
dt3 << New Column( "new_ort", Numeric, "Continuous", Format( "Best", 12 ), Formula( Round( 1 - (:ort), 2 ) ) ); // calculation and new column
dt3 << New Column( "new_rotg", Numeric, "Continuous", Format( "Best", 12 ), Formula( Round( 1 - (:rotg), 2 ) ) ); // calculation and new column
dt3 << New Column( "new_mag", Numeric, "Continuous", Format( "Best", 12 ), Formula( Round( 1 - (:mag - (-1.0)), 2 ) ) ); // calculation and new column
dt3 << New Column( "new_rotf", Numeric, "Continuous", Format( "Best", 12 ), Formula( Round( 1 - (:rotf - (-1.0)), 2 ) ) ); // calculation and new column
dtSum = dt3 << Summary(
Group( :mask ),
Mean( :new_x_offset, :new_y_offset, :new_xscl, :new_yscl, :new_ort, :new_rotg, :new_mag, :new_rotf ),
Freq( "None" ),
Weight( "None" )
);
您可能還想查看製表平台(分析=>製表),也可以使用它
