gpt4 book ai didi

matlab - accumarray 输出的解释

转载 作者:太空宇宙 更新时间:2023-11-03 20:27:55 24 4
gpt4 key购买 nike

我刚刚阅读了 accumarray 的文档,无法理解第二个示例。例子如下

val = 101:106';
subs = [1 1; 2 2; 3 2; 1 1; 2 2; 4 1]
subs =

1 1
2 2
3 2
1 1
2 2
4 1
A = accumarray(subs,val)
A =

205 0
0 207
0 103
106 0

如果我这样做

B=accumarray(subs(:,1),val)
C=accumarray(subs(:,2),val)

然后我得到

B=

205
207
103
106

C =

311
310

这对我来说是合乎逻辑的。但是为什么 B 的数字只是在 4x2 矩阵的“随机”位置排列(我想这不是随机的,但对我来说似乎是随机的)当我添加第二个时列到 subs?

最佳答案

摘自 accumarry 的 matlab 文档(注意:下面的引用来自 R2012a 文档,与 current version 不完全匹配)

The position of an element in subs determines which value of vals it selects for the accumulated vector; the value of an element in subs determines the position of the accumulated vector in the output.

因此,在您的示例中,“随机”排序来自 subs 指定的位置。分解 sub 的含义和最终结果我们得到这样的东西:

val = 101:106';
subs = [1 1; 2 2; 3 2; 1 1; 2 2; 4 1]
subs =

1 1 <-- take val(1) which is 101 and put it at position [1, 1] in the output
2 2 <-- put 102 in position [2, 2]
3 2 <-- put 103 in position [3, 2]
1 1 <--- ...and so on
2 2
4 1
A = accumarray(subs,val)
A =

205 0 <--- [1, 1] has value 101+104, [1, 2] has no value
0 207 <--- [2, 1] has no value, [2, 2] has value 102+105
0 103 <--- ...and so on
106 0

关于matlab - accumarray 输出的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42244491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com