You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matlab中将频率表转换为原始数据(tabulate函数逆操作)方法咨询

实现tabulate逆操作的常用方案

核心思路

遍历每一行的事件值和对应频次,跳过频次为0的行,将事件值重复对应频次后拼接即可。

各语言实现示例

Python

可以用numpy的repeat方法直接实现,输入为numpy二维数组:

import numpy as np

# 示例输入矩阵
freq_mat = np.array([[0,4],[1,2],[2,2],[3,1],[4,0]])
# 过滤掉频次为0的行,然后重复对应次数
raw_data = np.repeat(freq_mat[:,0], freq_mat[:,1].astype(int))

输出结果为array([0, 0, 0, 0, 1, 1, 2, 2, 3]),完全符合需求。

Matlab

和numpy逻辑一致,用内置的repelem函数实现:

% 示例输入矩阵
freq_mat = [0 4; 1 2; 2 2; 3 1; 4 0];
% 自动跳过0频次项,直接生成原始数组
raw_data = repelem(freq_mat(:,1), freq_mat(:,2));

R

用基础的rep函数即可实现:

# 示例输入矩阵
freq_mat <- matrix(c(0,4,1,2,2,2,3,1,4,0), ncol=2, byrow=TRUE)
# 重复对应次数生成原始数组
raw_data <- rep(freq_mat[,1], freq_mat[,2])

内容的提问来源于stack exchange,提问作者Matt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 23:42:03