Effacer les filtres
Effacer les filtres

indexing through array in for loop

2 vues (au cours des 30 derniers jours)
Emu
Emu le 30 Oct 2022
Modifié(e) : Star Strider le 30 Oct 2022
hi - I am trying to generate an input matric for the Cohen's Kappa function. I have some code which does this iteratively - that is goes through all combinations of my coding key (0-16) and sums them, but am trying to find a way to loop through all combinations of the coding key (0,0; 0,1; 0,2 etc), rather than write out all 289 instances.
for row = 1:length(whoSpeakMaster) %loop through the coded stage 1 output
if whoSpeakMaster(row,1) == 0 && whoSpeakCoder(row,1) == 0 %identify how many instances of master and coder coding 1
speakerBb11 = speakerBb11+1; %sum((whoSpeakMaster(row,1) == 0)) %&& whoSpeakCoder == 0));%adds to the counter
end
if whoSpeakMaster(row,1) == 0 && whoSpeakCoder(row,1) == 1
speakerBb12 = speakerBb12 +1;
end
end
  2 commentaires
Star Strider
Star Strider le 30 Oct 2022
The κ statistic is for inter-rater reliability.
What are your original data, and what do you want to do with them?
Emu
Emu le 30 Oct 2022
Yes - so for each instance I will have two datasets (from two seperate coders/raters - whoSpeakMaster and whoSpeakCoder - attached) which have each been coded/rated on each row from a coding scheme from 0-16. So I just want to generate from them how many times coder 1 has rated '0' and coder 2 has rated '0' and so on for all combinations.

Connectez-vous pour commenter.

Réponses (1)

Star Strider
Star Strider le 30 Oct 2022
What function are you using to estimate the κ statistic? I don’t see any built-in MATLAB functions for it, although I may be looking in the wrong place. (I had to write my own function for it when I used it a few decazdes ago.)
This has only one variable.
My choice would be to use accumarray (since I have a fair amount of experience with it), however other options also exist.
One approach —
LD = load(websave('whoSpeakCoder','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1173943/whoSpeakCoder.mat'));
Coder = LD.whoSpeakCoder;
[UCoder,~,ix] = unique(Coder);
Tally = accumarray(ix, 1, []);
Out = table(UCoder,Tally)
Out = 11×2 table
UCoder Tally ______ _____ 0 2 1 119 2 70 3 3 5 25 6 417 8 4 9 35 12 4 13 9 999 92
The ‘UCoder’ vector are simply the sorted unique values that appear in ‘Coder’.
.
  2 commentaires
Emu
Emu le 30 Oct 2022
thank you , i used groupcounts to do this, but I need the values across two datasets (2nd one attached). The way the function works is explained in the discussion by the author:
"Cohen's test always uses a square matrix of dimension k x k, where k is the number of classes in which the two judges can rank an instance. we only know that there are 100 instances, but how many classes? if, for example, they were 3 classes, then you would have a 3 x 3 matrix (which is a square matrix). In cell (1,1) will go the number of instances classified as 1 by both judges, in cell (1,2) will go the number of instances classified as 1 by the first judge and 2 by the second judge, and so on. The sum of the elements of the matrix will be equal to the number of instances (100 in this case) and on the diagonal there will be the numbers of instances classified identically by both judges."
Star Strider
Star Strider le 30 Oct 2022
Modifié(e) : Star Strider le 30 Oct 2022
There are two NaN values in ‘Master’ that are going to cause problems. I defer to you to solve them.
Otherwise, it’s accumarray to the rescue again!
Try this —
LD1 = load(websave('whoSpeakCoder','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1173943/whoSpeakCoder.mat'));
Coder = LD1.whoSpeakCoder;
LD2 = load(websave('whoSpeakMaster','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1173963/whoSpeakMaster.mat'));
Master = LD2.whoSpeakMaster;
NrNaN = nnz(isnan(Master))
NrNaN = 2
[UCoder,~,ixc] = unique(Coder);
[UMaster,~,ixm] = unique(Master);
% UCoder
% UMaster
Tally = accumarray([ixc ixm], 1, [])
Tally = 11×10
0 0 2 0 0 0 0 0 0 0 3 22 54 29 5 6 0 0 0 0 0 16 31 14 6 3 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 1 0 23 1 0 0 0 0 0 0 65 215 86 16 27 2 4 1 1 0 2 2 0 0 0 0 0 0 0 0 0 5 26 2 2 0 0 0 0 0 0 2 2 0 0 0 0 0 0 0 2 0 7 0 0 0 0 0 0
[r,c] = size(Tally);
TallyM = zeros(r+1,c+1);
TallyM(2:end,1) = UCoder;
TallyM(1,2:end) = UMaster.';
TallyM(2:end,2:end) = Tally % 'Tally' Matrix With Row & Column Designations Added
TallyM = 12×11
0 0 1 2 5 6 8 9 12 NaN NaN 0 0 0 2 0 0 0 0 0 0 0 1 3 22 54 29 5 6 0 0 0 0 2 0 16 31 14 6 3 0 0 0 0 3 0 0 0 1 1 0 1 0 0 0 5 0 1 0 23 1 0 0 0 0 0 6 0 65 215 86 16 27 2 4 1 1 8 0 2 2 0 0 0 0 0 0 0 9 0 0 5 26 2 2 0 0 0 0 12 0 0 2 2 0 0 0 0 0 0
% VN = compose('M%2d',UMaster);
% RN = compose('C%2d',UCoder);
% Out = array2table(Tally, 'VariableNames',VN, 'RowNames',RN)
Tally16 = zeros(16); % Matrix With All 16 Categories
Tally16(UCoder(1:end-1)+1,UMaster(~isnan(UMaster))+1) = Tally(1:end-1,1:end-2)
Tally16 = 16×16
0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 3 22 54 0 0 29 5 0 6 0 0 0 0 0 0 0 0 16 31 0 0 14 6 0 3 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 23 1 0 0 0 0 0 0 0 0 0 0 65 215 0 0 86 16 0 27 2 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 0 26 2 0 2 0 0 0 0 0 0 0
Tally16M = zeros(17);
Tally16M(1,2:end) = 1:16;
Tally16M(2:end,1) = 1:16
Tally16M = 17×17
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Tally16M(2:end, 2:end) = Tally16
Tally16M = 17×17
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 2 3 22 54 0 0 29 5 0 6 0 0 0 0 0 0 0 3 0 16 31 0 0 14 6 0 3 0 0 0 0 0 0 0 4 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0 1 0 0 0 23 1 0 0 0 0 0 0 0 0 0 7 0 65 215 0 0 86 16 0 27 2 0 0 4 0 0 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 0 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0
NOTES —
I was going to create a table with the results here too, however the NaN values made that impossible, because they throw a ‘repeated variable name’ error..
There are 11 ‘Coder’ categories in ‘UCoder’ and 10 ‘Master’ categories in ‘UMaster’ so the ‘Master’ categories are across the top (colums) and the Coder categories are the rows. See ‘TallyM’ for those details. (The ‘TallyM’ (1,1) element has no meaning. It is just there to fill the gap.)
EDIT — (30 Oct at 15:52)
Added 16-category ‘Tally16’ and ‘Tally16M’ matrices.
.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by