create table that count the composition of two categories.
2 次查看(过去 30 天)
显示 更早的评论
Hello,
I have a question. I have two vectors of 562 x 1. Both belongs to a dataset.
As example
column 1 - column 2
Red - circle
Blue - square
Yellow - circle
Blue - square
How can I make an overview that it counts how many red's are linket to the circle etc. like this:
Red Blue Yellow
Circle 1 0 1
Square 0 2 0
First question: What is the best way for input?
like combine them as 1 vector (562 x 2) or something else?
Second question, with the right input how can I create a nice overview of this?
Thanks a lot in advance.
With kind regard,
John
0 个评论
回答(4 个)
Bob Thompson
2019-3-8
Loading things with one command is generally always preferred, so yes, combining them into a 562x2 array is helpful.
Once read in you can use a second table with your column and row headers to store the counts of the different parameters. Summing them is not difficult, just use sum() and logic indexing.
nred = sum(strcmp(data(:,1),'Red') & strcmp(data(:,2),'square'));
0 个评论
Peter Perkins
2019-3-12
This is almost an unstack operation. It may be that all you want is a nice summary output, not sure. But often this kind of thing is data pre-processing, and unstack on a tabkle might suit your needs. First thing I'd do is make those shapes and colors categoricals:
>> t = table(["R";"B";"Y";"B"],["c";"s";"c";"s"],'VariableNames',["Color" "Shape"])
t =
4×2 table
Color Shape
_____ _____
"R" "c"
"B" "s"
"Y" "c"
"B" "s"
>> t.Color = categorical(t.Color);
>> t.Shape = categorical(t.Shape);
To use unstack, you'll need to add a column of ones. It's a little awkward, but it makes unstack easy:
>> t.Ones = ones(height(t),1)
t =
4×3 table
Color Shape Ones
_____ _____ ____
R c 1
B s 1
Y c 1
B s 1
>> unstack(t,'Ones','Color','GroupingVariable','Shape')
ans =
2×4 table
Shape B R Y
_____ ___ ___ ___
c NaN 1 1
s 2 NaN NaN
Those are NaNs, not zeros, which may not be what you need. For data preprocessing, it makes some sense.
0 个评论
John Verdonschot
2019-3-13
2 个评论
Peter Perkins
2019-3-13
You are using a version of MATLAB prior to R2018a (IIRC) that does not support strings for the parameter names/values. change ["R"; ...] etc. to {'R'; ...} etc. and that will work.
Sorry, I'm always on the newest.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!