问题描述
我正在跟进我的问题此处,其中有一个完美的解决方案完全可以满足我的要求.但是我想知道如何应用这种方法,或者做类似的事情,如果我将有两个以上的响应,而不是是/否,那么例如是/否.或将其推广到3个以上的响应中.
I am asking a follow-up my question here, in which there was a perfect solution that did exactly what I wanted. But I'm wondering how to apply this method, or do something similar, if instead of yes/no as possible responses, I would have more than 2 responses, so yes/no/maybe, for example. Or how it would generalize to 3+ responses.
这是答案,重新格式化为我的问题:
This is the answer, reformatted as my question:
假设我的数据如下:
responses = categorical(randi(3,1250,1),[1 2 3],{'no','yes','maybe'});
race = categorical(randi(5,1250,1),1:5,{'Asian','Black','BHispanic','White','WHispanic'});
我想对是/否数据做同样的事情,但是用3种或更多的可能性来做.这将最终无法正常工作:
I would like to go through and do the same thing with my yes/no data, but do this with 3 possibilities, or more. And this will not end up working anymore:
% convert everything to numeric:
yn = double(responses);
rac = double(race);
% caluculate all frequencies:
data = accumarray(rac,yn-1);
data(:,2) = accumarray(rac,1)-data;
% get the categories names:
races = categories(race);
answers = categories(responses);
% plotting:
bar(data,0.4,'stacked');
ax = gca;
ax.XTickLabel = races; % set the x-axis ticks to the race names
legend(answers) % add a legend for the colors
colormap(lines(3)) % use nicer colors (close to your example)
ylabel('YES/NO/MAYBE')% set the y-axis label
% some other minor fixes:
box off
ax.YGrid = 'on';
我不确定是否甚至可以使用accumarray方法来执行此操作,因为根据我的理解,将其与3种可能的响应一起使用是没有意义的.我也想将其概括为n个可能的响应.
I'm not sure if there is even a way to use the accumarray method to do this, as it doesn't make sense from my understanding to use this with 3 possible responses. I'd like to generalize it to n possible responses too.
更新:我目前正在研究交叉表功能,到目前为止我才发现它!我认为这可能是我要寻找的功能.
UPDATE: I'm currently investigating the crosstab feature which I didn't find at all until now! I think this may be the feature I'm looking for.
推荐答案
以下是通用版本:
% the data (with even more categories):
yesno = categorical(randi(4,1250,1),1:4,{'no','yes','maybe','don''t know'});
race = categorical(randi(5,1250,1),1:5,{'Asian','Black','BHispanic','White','WHispanic'});
% convert everything to numeric:
yn = double(yesno);
rac = double(race);
% caluculate all frequencies:
data = accumarray([rac yn],1);
% get the categories names:
races = categories(race);
answers = categories(yesno);
% plotting:
bar(data,0.4,'stacked');
ax = gca;
ax.XTickLabel = races; % set the x-axis ticks to the race names
legend(answers) % add a legend for the colors
colormap(lines(numel(answers))) % use pretier colors
ylabel('YES/NO')% set the y-axis lable
% some other minor fixes:
box off
ax.YGrid = 'on';
结果:
在表格中:
T = array2table(data.','VariableNames',races,'RowNames',answers)
输出:
T =
Asian Black BHispanic White WHispanic
_____ _____ _________ _____ _________
no 58 72 69 66 62
yes 58 53 72 54 58
maybe 63 62 67 62 61
don't know 58 57 66 58 74
如前所述,您可以将crosstab
用于同一任务. crosstab(rac,yn)
将为您提供与accumarray([rac yn],1)
相同的结果.我认为accumarray
更快,尽管我没有检查.
As you already mentioned, you can use crosstab
for the same task. crosstab(rac,yn)
will give you the same result as accumarray([rac yn],1)
. I think accumarray
is faster, though I didn't check it.
这篇关于MATLAB-使用具有多个类别的accumarray吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!