create table that count the composition of two categories.
조회 수: 4 (최근 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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Categorical Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!