create table that count the composition of two categories.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (4)
Bob Thompson
am 8 Mär. 2019
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 Kommentare
Peter Perkins
am 12 Mär. 2019
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 Kommentare
John Verdonschot
am 13 Mär. 2019
2 Kommentare
Peter Perkins
am 13 Mär. 2019
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.
Siehe auch
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!