indexing through array in for loop
3 views (last 30 days)
Show older comments
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 Comments
Star Strider
on 30 Oct 2022
The κ statistic is for inter-rater reliability.
What are your original data, and what do you want to do with them?
Answers (1)
Star Strider
on 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)
The ‘UCoder’ vector are simply the sorted unique values that appear in ‘Coder’.
.
2 Comments
Star Strider
on 30 Oct 2022
Edited: Star Strider
on 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))
[UCoder,~,ixc] = unique(Coder);
[UMaster,~,ixm] = unique(Master);
% UCoder
% UMaster
Tally = accumarray([ixc ixm], 1, [])
[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
% 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)
Tally16M = zeros(17);
Tally16M(1,2:end) = 1:16;
Tally16M(2:end,1) = 1:16
Tally16M(2:end, 2:end) = Tally16
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.
.
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!