71739

MATLAB - Working with accumarray with multiple categories?

Question:

I am asking a follow-up my question <a href="https://stackoverflow.com/questions/42312725/plotting-two-categorical-arrays-in-a-histogram-bar-chart" rel="nofollow">here</a>, 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:

Assuming my data looks like this:

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'});

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';

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.

Answer1:

Here is a generalized version:

% 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';

The result:

<a href="https://i.stack.imgur.com/HZn2P.png" rel="nofollow"><img alt="bar" class="b-lazy" data-src="https://i.stack.imgur.com/HZn2P.png" data-original="https://i.stack.imgur.com/HZn2P.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

And in a table:

T = array2table(data.','VariableNames',races,'RowNames',answers)

the output:

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 <hr />

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.

Recommend

  • ñ to Ñ string php
  • Why is favicon not visible
  • How to calculate the number of occurrences of each element in the 100000 vectors using Matlab? [dupl
  • Generating 6 digit unique random number generator sequence in oracle
  • JDBC Thin connection string in Oracle uses both colon and forward slash
  • Capifony fails deploying
  • Are XHTML self closing elements still valid in HTML5?
  • Curl and relative path in
  • Convert 64 bit numbers from binary to decimal using uint64
  • Eclipse Sequoyah Cannot debug native JNI code: No connection machine actively refused it
  • Audio sampling rate of an ip camera and FFT
  • Find frequencies over 3rd quartile in table
  • javascript find and replace a dynamic pattern in a string
  • find the repetition of duplicate numbers
  • R: Cleaning up a wide and untidy dataframe
  • How can I map an enum to a boolean with mapstruct?
  • Perl - Using regex to match input in hash key or value
  • foldr and foldr1 Haskell
  • How to get new selected and old values after user selected option from dropdown list box?
  • Designing a simple bandpass/bandstop filter in Matlab
  • In javascript, what is a constructor? And what isn't?
  • Sparse matrix in matlab: set unrecorded elements to -1 instead of 0
  • QMenu: Set text color for specific QAction
  • Can't run multiple batch files sequentially from master batch file in Win7
  • Mean of columns with same label
  • Finding regular expressions for languages otherwise described
  • Writing a recursive function on lists in Haskell
  • C++ stl pop doesn't return [closed]
  • How to remove last utf8 char of a python string
  • Unable to set a breakpoint on main while debugging a program compiled with Rust 1.10 with GDB
  • Generating anchors with PyYAML.dump()?
  • jQuery: add elements until a particular height is reached
  • Use of this Javascript
  • Linq Objects Group By & Sum
  • How to format a variable of double type
  • Matrix multiplication with MKL
  • Buffer size for converting unsigned long to string
  • Hits per day in Google Big Query
  • File not found error Google Drive API
  • Converting MP3 duration time