How do I convert a frequency table to a single vector?

4 ビュー (過去 30 日間)
Aaron Goodman
Aaron Goodman 2022 年 1 月 19 日
コメント済み: Aaron Goodman 2022 年 1 月 19 日
I have the following frequecy table where Column 1 is a parameter I am measuring, and column 2 is the number of times that parameter occurred:
I would like to convert this to a single vector:
x = [3 4 4 4 4 4 4 4 4 6 23]
so I can perform statistical tests on the data. Does anyone have a simple solution?
Thank you!

採用された回答

Voss
Voss 2022 年 1 月 19 日
First, setting up the variable as you have it:
Blank2 = zeros(23,2);
Blank2(:,1) = 1:23;
Blank2(3,2) = 1;
Blank2(4,2) = 8;
Blank2(6,2) = 1;
Blank2(23,2) = 1;
display(Blank2);
Blank2 = 23×2
1 0 2 0 3 1 4 8 5 0 6 1 7 0 8 0 9 0 10 0
One way is to use arrayfun() to operate on the elements of each column:
x = arrayfun(@(x,y)x*ones(1,y),Blank2(:,1),Blank2(:,2),'UniformOutput',false);
x = [x{:}];
display(x);
x = 1×11
3 4 4 4 4 4 4 4 4 6 23
Another equivalent way using a for loop:
x = [];
for i = 1:size(Blank2,1)
x = [x Blank2(i,1)*ones(1,Blank2(i,2))];
end
display(x);
x = 1×11
3 4 4 4 4 4 4 4 4 6 23

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by