I have a matrix whose rows represent music notes. [A,A#,B,C,C#,D,D#,E,F,F#,G,G#] So index 1 represents A, 2 represents A# and so on. In the matrix, the largest the element in that index the more likely it is to be present in the audio.
I want to calculate which 3 indexes are appearing most frequently between the possible 12.
This is how I reasoned it out. First I sort every column using [B,I] = sort().. B now contains the sorted column I now contains the indexes sorted in an ascending manner.
Therefore I[10], I[11] and I[12] now contain the 3 largest indexes in that column.
Now I create a new vector called notes. If a particular note when sorted, finds itself among the largest 3 indexes of I, then the respective note should be incremented.
I'm trying for the first 3 columns, which are these:
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
Hence in B for the first loop iteration I should get,
0.0392
0.0572
0.0674
0.0690
0.0772
0.0794
0.1182
0.1628
0.2453
0.4119
0.7473
1.2037
which I'm getting (so till now everything's ok). I (which contains the sorted indexes) is being returned correctly too with the 3 largest indexes being 8, 11, 4 (ie. the original indexes that contained the largest element where 8, 11, 4 in ascending order)
The problem is within the if condition. In the first iteration, After the if condition, the 'notes' column vector should be incremented in the 8th place, 11th place, 4th place ie.
0
0
0
1
0
0
0
1
0
0
1
0
After the if condition however, only the 4th place in the vector is being incremented. In fact after first iteration, when displaying the 'notes' vector, I am getting
0
0
0
1
0
0
0
0
0
0
0
0
And this is the code:
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);
What am I doing wrong? The code may be wrong (or perhaps it could be better). I am in no means good at Matlab. This is the first time using it.
Would appreciate your ideas, opinions, corrections.
Thank you for your time in advance