How to generate joint frequency table?

2 ビュー (過去 30 日間)
Rouyu Chen
Rouyu Chen 2023 年 4 月 12 日
コメント済み: Rouyu Chen 2023 年 4 月 12 日
Dear experts,
I have two vectors and I want to make a joint frequency table, I tried tabulate but failed, I was wondering what is wrong with my code, and if there is any other code can help me do this?
Many thanks!
Below is the code I tried:
% Two vectors
Attribute
Assignment_result
% Combine them together
data=table(Attribute,Assignment_result)
% Tabulate
joint_freq=tabulate(data.Attribute,data.Assignment_result)
disp(joint_freq)
But matlab suggests 'too many input arguments'
  3 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 4 月 12 日
tabulate() accepts only 1 input, whereas you are giving 2 inputs to it.
What are you trying to do? Calculating the frequency of 1 vector w.r.t to other?
In any case, please attach your data (use the paperclip icon to do so), and mention the output you want to obtain.
Rouyu Chen
Rouyu Chen 2023 年 4 月 12 日
Thank you Jon and Dyuman Joshi!

サインインしてコメントする。

採用された回答

the cyclist
the cyclist 2023 年 4 月 12 日
It isn't perfectly clear what you mean, but I think this does what you intend.
% Set the seed, for reproduciblity
rng default
% Two example vectors
vector1 = randi([1, 3], 5, 1);
vector2 = randi([1, 3], 5, 1);
% Combine the vectors into a matrix
data = [vector1, vector2]
data = 5×2
3 1 3 1 1 2 3 3 2 3
% Unique rows and their indices
[unique_rows, ~, row_indices] = unique(data, 'rows');
% Frequency of each unique row
freq = histcounts(row_indices, 1:size(unique_rows,1)+1);
% Combine the unique rows and their frequencies into a table
freq_table = table(unique_rows, freq', 'VariableNames', {'UniqueRows', 'Frequency'})
freq_table = 4×2 table
UniqueRows Frequency __________ _________ 1 2 1 2 3 1 3 1 2 3 3 1
  3 件のコメント
the cyclist
the cyclist 2023 年 4 月 12 日
Ah, you want a cross table:
% Set the seed, for reproduciblity
rng default
% Two example vectors
vector1 = randi([1, 3], 5, 1);
vector2 = randi([1, 3], 5, 1);
[tbl,chi2,p,labels] = crosstab(vector1,vector2)
tbl = 3×3
0 1 0 0 0 1 2 0 1
chi2 = 6.6667
p = 0.1546
labels = 3×2 cell array
{'1'} {'1'} {'2'} {'2'} {'3'} {'3'}
Rouyu Chen
Rouyu Chen 2023 年 4 月 12 日
This is exactly what I am looking for, thank you so much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFoundation and Custom Domains についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by