追加到在Matlab重复元件的阵列 [英] Appending to an array of repeated elements in matlab
问题描述
的数据变量是一个3X9阵列,其中第一行是电压,第二行是计数和第三行是错误。我想在每一个电压平均计数/错误,使处理后的数组是一样的东西。
The data variable is a 3x9 array, where the first row is voltage, the second row is counts, and the third row is error. I want to average the counts/errors at each voltage so that the processed array is something like
combined = [1,2,3;.99,.1.2,1.3;.2,.3,.5]
我尝试迄今:
data = [1,1,1,2,2,2,3,3,3;...
.98,.99,.98,1.1,1.2,1.2,1.3,1.3,1.4;...
.3,.2,.2,.3,.3,.4,.5,.4,.5];
volt = data(1,:);
v_uniq=unique(volt);%Array of unique voltage values
for k=1:length(v_uniq)
volt_i=find(volt==v_uniq(k));%Indices for set of repeating values
combined_m(k)=mean(data(2,volt_i(1):volt_i(end)).')';%averaged means
combined_e(k)=mean(data(3,volt_i(1):volt_i(end)).')';%averaged error
combined(k) = [combined_m(k);combined_e(k)];
%combined(k)=combined(k);
end
具有挑战性的部分被追加每次迭代后的综合阵列,因为v_uniq本身就是一个数组,并导致问题试图遍历时。
The challenging part is appending the combined array after each iteration, since v_uniq is an array itself and causes problems when trying to iterate through.
有没有一些方法来组合阵列追加,而不必指数v_uniq?或者,如果也没有,有另一种,要解决这个问题更简单的方法?
Is there some way to append the combined array without having to index v_uniq? Or if nor, is there another, simpler way to approach this problem?
推荐答案
如果你想获得你的code工作,用丹尼尔的建议,在他的评论对你上面会工作。只需将为
循环code的最后一行替换这样的:
If you want to get your code working, using Daniel's suggestion in his comment to you above will work. Simply replace the last line of your for
loop code to this:
combined(:,k) = [combined_m(k);combined_e(k)];
您想插入一列,显示每个独特的电压组合计数和错误。
You want to insert a column that shows the combined counts and errors for each unique voltage.
不过,我会建议使用 accumarray
,而不是你的为
循环方式,并使用电压作为密钥,并用你的矩阵的另两行的值。如何 accumarray
工作原理是,你有两个数组:键
和值
。对于存储在键的每个值
,我们看到对应的号码是什么值
,我们把这个号码成由键
收录的垃圾桶。一旦我们做到这一切分级的,你再使用结合了所有每斌这些条目在一起的功能。默认情况下 accumarray
使用之
,所以你只是总结所有被映射到每个仓在一起值
However, I would recommend using accumarray
instead of your for
loop approach, and use the voltage as the key and use the other two rows of your matrix as values. How accumarray
works is that you have two arrays: keys
and values
. For each value that is stored in keys
, we see what the corresponding number is in values
, and we place this number into a bin that is indexed by keys
. Once we do all of this binning, you then use a function that combines all of these entries per bin together. By default accumarray
uses sum
, and so you'd just sum all of the values that get mapped to each bin together.
不过,对于你的情况,你会使用的意思是
作为函数应用在每个仓的找到每一个独特的电压的平均误差和计数。事情是这样的:
However, for your case you would use mean
as the function to apply over each of the bins to find the average error and counts for each unique voltage. Something like this:
data = [1,1,1,2,2,2,3,3,3;...
.98,.99,.98,1.1,1.2,1.2,1.3,1.3,1.4;...
.3,.2,.2,.3,.3,.4,.5,.4,.5];
ave_counts = accumarray(data(1,:).', data(2,:).', [], @mean);
ave_error = accumarray(data(1,:).', data(3,:).', [], @mean);
combined = [ave_counts ave_error];
在 accumarray
,第一个参数是关键,这是在我们的例子中的电压,而<code>值是无论是计数或错误 - 基本上那些需要被组合值
In accumarray
, the first argument is the keys, which are the voltages in our case, and values
are either the counts or the errors - essentially those values that need to be combined.
ave_counts
将包含每个独特的电压,平均计数,而 ave_error
将包含每个独特的电压的平均误差。然后,我们可以将这些成2列矩阵,比如code最后行创建组合
,正如在code的看到。
ave_counts
will contain the average counts per unique voltage while ave_error
will contain the average error per unique voltage. We can then combine these into a 2 column matrix like the last line of code to create combined
, as what was seen in your code.
由于你的电压以增加的方式已订购,这将意味着,每个元素对应于该电压恰好。运行此之后,这是我得到 ave_counts
和 ave_error
:
Because your voltages are already ordered in an increasing way, this will mean that each element corresponds to that voltage exactly. After running this, this is what I get for ave_counts
and ave_error
:
>> ave_counts
ave_counts =
0.9833
1.1667
1.3333
>> ave_error
ave_error =
0.2333
0.3333
0.4667
这表示,为电压= 1
,平均数和平均误差为 0.9833
和 0.2333
分别。您可以通过手工计算此验证这一点。前三计数和错误是电压= 1
,如果我们计算的平均误差和计数,我们得到:
This says that for voltage = 1
, the average count and average error are 0.9833
and 0.2333
respectively. You can verify this by calculating this by hand. The first three counts and errors are for voltage = 1
, and if we calculate the average error and counts, we get:
(0.98+0.99+0.98) / 3 = 0.9833 <-- Average count for voltage = 1
(0.3+0.2+0.3) / 3 = 0.2333 <-- Average error for voltage = 1
同样,对于电压= 2
:
(1.1+1.2+1.2) / 3 = 1.1667 <-- Average count for voltage = 2
(0.3+0.3+0.4) / 3 = 0.3333 <-- Average error for voltage = 2
终于为电压= 3
:
(1.3+1.3+1.4) / 3 = 1.3333 <-- Average count for voltage = 3
(0.5+0.4+0.5) / 3 = 0.4667 <-- Average error for voltage = 3
这是我们手工上面计算的量是什么 accumarray
这两个量输出。
The quantities that we calculated above by hand are exactly what accumarray
outputs for both quantities.
accumarray
仅设计采取的整数键。如果你有浮点值,你仍然可以使用 accumarray
,但你必须首先做一些pre-处理。我会做的是使用唯一
和专门使用的第三个输出。第三输出取入输入的每个唯一值和一个整数ID分配给它。这是相同的获得分配的相同的整数ID ,它是这样理想的输入 accumarray
的任何值。你还需要第一输出跟踪电压正在每结合
矩阵的行平均什么。
accumarray
is only designed to take in integer keys. If you have floating-point values, you can still use accumarray
, but you'd have to do some pre-processing first. What I would do is use unique
and use the third output specifically. The third output takes each unique value in the input and assigns an integer ID to it. Any values that are the same get assigned the same integer ID, which is thus ideal as input into accumarray
. You would also need the first output to keep track of what voltages are being averaged per row of the combined
matrix.
您将因此使用第三输出唯一的
输入到 accumarray
。因此,做这样的事情:
You would thus use the third output of unique
as input into accumarray
. Therefore, do something like this:
[voltages,~,id] = unique(data(1,:));
ave_counts = accumarray(id, data(2,:).', [], @mean);
ave_error = accumarray(id, data(3,:).', [], @mean);
combined = [ave_counts ave_error];
在这种情况下,每个元素 ave_counts
和 ave_error
将对应于存储在同一个元素电压
。因此,电压(1)
将对应于组合
,电压的第一行( 2)
将对应于第二行组合
,等等。
In this case, each element of ave_counts
and ave_error
will correspond to the same element stored in voltages
. As such, voltages(1)
will correspond to the first row of combined
, voltages(2)
will correspond to the second row of combined
, and so on.
有关完整性,如果我们运行上面的示例数据,这就是我们得到电压
和组合
:
For completeness, if we ran the above with your example data, this is what we get for voltages
and combined
:
>> voltages
voltages =
1 2 3
>> combined
combined =
0.9833 0.2333
1.1667 0.3333
1.3333 0.4667
因此,电压= 1
为我们提供了 0.9833
的平均计数与平均误差 0.2333
,等等。
Therefore, voltage = 1
gives us an average count of 0.9833
with an average error of 0.2333
, and so on.
这篇关于追加到在Matlab重复元件的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!