如何在matlab中将一组曲线切片并存储在多个变量中?

How to slice and store a set of curves in multiple variables in matlab?

我有一个 table 有 2 列(x 和 y 值)和 100 行(x 值在一定间隔内重复)。

因此,我想对可变 table 大小执行以下任务(仅更改行大小!):

I want to determine the amount of repetitions of the x values and save this information as a variable named n. Here the amount of repetition is 5 (each x value occurs 5 times in total).

I want to know the range of the x values from repetition circle and save this information as R = height(range); Here the x range is [0,20]

根据以上信息,我希望创建更小的 tables,其中只存在一个重复的 x 值

如何在 matlab 中实现它?

保持安全和健康,

葛丽泰

此方法使用 table2array() 函数将 Table 转换为 array/matrix 以进行进一步处理。为了找到 x 值中的重复模式,unique() 函数用于检索重复多次的向量。值的范围可以通过使用 min()max() 函数并连接 2 元素数组中的值来计算。然后可以使用 assignin() 函数创建一组较小的表格,这些表格根据 x 值重复来分隔 y 值。

Table 用于测试脚本:

x = repmat((1:20).',[5 1]);
y = rand(100,1);
Table = array2table([x y]);

脚本:

Array = table2array(Table);
Unique_X_Values = unique(Array(:,1));

Number_Of_Repetitions = length(Array)/length(Unique_X_Values);

Range = [min(Array(:,1)) max(Array(:,1))];

Y_Reshaped = reshape(Array(:,2),[numel(Array(:,2))/Number_Of_Repetitions Number_Of_Repetitions]);

for Column_Index = 1: Number_Of_Repetitions
    Variable_Name = ['Small_Tables_' num2str(Column_Index)];
    assignin('base',Variable_Name,array2table(Y_Reshaped(:,Column_Index)));
    eval(Variable_Name);
end

fprintf("Number of repetitions: %d\n",Number_Of_Repetitions);
fprintf("Range: [%d,%d]\n",Range(1),Range(2));