【发布时间】:2015-03-08 21:12:51
【问题描述】:
我有一个矩阵,其行代表音符。 [A,A#,B,C,C#,D,D#,E,F,F#,G,G#] 所以索引 1 代表 A,2 代表 A#,以此类推。在矩阵中,该索引中最大的元素越有可能出现在音频中。
我想计算这 3 个索引在可能的 12 个索引中出现的频率最高。
这就是我的推理方式。首先,我使用 [B,I] = sort() 对每一列进行排序。 B 现在包含排好序的列 I 现在包含按升序排序的索引。
因此,I[10]、I[11] 和 I[12] 现在包含该列中的 3 个最大索引。
现在我创建一个名为 notes 的新向量。如果一个特定的注释在排序时发现自己位于 I 的最大 3 个索引中,则相应的注释应该递增。
我正在尝试前 3 列,它们是:
0.0690 0.0530 0.0656
0.2453 0.2277 0.2306
0.0647 0.0315 0.0494
1.2037 1.1612 1.1613
0.0772 0.0346 0.0367
0.1628 0.1429 0.1648
0.0572 0.0370 0.0493
0.4119 0.3577 0.3635
0.0392 0.0430 0.0466
0.1182 0.0921 0.0935
0.7473 0.6680 0.7088
0.0794 0.0527 0.0566
因此在 B 中我应该得到的第一个循环迭代,
0.0392
0.0572
0.0674
0.0690
0.0772
0.0794
0.1182
0.1628
0.2453
0.4119
0.7473
1.2037
我得到了(所以到目前为止一切都很好)。 I(包含排序的索引)也被正确返回,3 个最大的索引是 8、11、4(即包含最大元素的原始索引,其中 8、11、4 按升序排列)
问题在于 if 条件。在第一次迭代中,在 if 条件之后,'notes' 列向量应该在第 8 位、第 11 位、第 4 位递增,即。
0
0
0
1
0
0
0
1
0
0
1
0
然而,在 if 条件之后,只有向量中的第 4 位被递增。事实上,在第一次迭代之后,当显示“notes”向量时,我得到了
0
0
0
1
0
0
0
0
0
0
0
0
这是代码:
for col = 1:3
[B,I] = sort(C(:,col), 'ascend'); %B is the sorted column. I is the sorted indexes
fprintf('Col No:');
disp(col);
fprintf('Sorted Column: ');
disp(B);
fprintf('Sorted Indexes: ');
disp(I);
if (I(10) == 1 || I(11) == 1 || I(12) == 1)
notes(1,:) = notes(1,:) + 1;
elseif (I(10) == 2 || I(11) == 2 || I(12) == 2)
notes(2,:) = notes(2,:) + 1;
elseif (I(10) == 3 || I(11) == 3 || I(12) == 3)
notes(3,:) = notes(3,:) + 1;
elseif (I(10) == 4 || I(11) == 4 || I(12) == 4)
notes(4,:) = notes(4,:) + 1;
elseif (I(10) == 5 || I(11) == 5 || I(12) == 5)
notes(5,:) = notes(5,:) + 1;
elseif (I(10) == 6 || I(11) == 6 || I(12) == 6)
notes(6,:) = notes(6,:) + 1;
elseif (I(10) == 7 || I(11) == 7 || I(12) == 7)
notes(7,:) = notes(7,:) + 1;
elseif (I(10) == 8 || I(11) == 8 || I(12) == 8)
notes(8,:) = notes(8,:) + 1;
elseif (I(10) == 9 || I(11) == 9 || I(12) == 9)
notes(9,:) = notes(9,:) + 1;
elseif (I(10) == 10 || I(11) == 10 || I(12) == 10)
notes(10,:) = notes(10,:) + 1;
elseif (I(10) == 11 || I(11) == 11 || I(12) == 11)
notes(11,:) = notes(11,:) + 1;
elseif (I(10) == 12 || I(11) == 12 || I(12) == 12)
notes(12,:) = notes(12,:) + 1;
end
disp(notes);
我做错了什么?代码可能是错误的(或者可能会更好)。我并不擅长 Matlab。这是第一次使用它。
非常感谢您的想法、意见和更正。
提前感谢您的宝贵时间
【问题讨论】: