首页 > 解决方案 > 连续标记数据块

问题描述

我有数据包含在 4195X1 double 中,称为 z1。我想提取 120 个块中的数据并使用 matlab 将它们标记为 z1_1_120、z1_120_240、z1_240_360 等。我想在循环中提取它们并以这种方式标记它们。这是我到目前为止所做的,但不确定如何进行:

load(z1)
for i = 1:4195
    q=z1(i);
    q1(i,:)=q;
    q2=q1(1:120:end);
end
z1=q2(1:end);

标签: matlabfor-loopextract

解决方案


正如丹尼尔所说,不要这样做!

如果您想要您描述的那种标签,您可以使用 Table 或 Struct 数据类型。

阅读以下帖子

解决方案很简单:不要这样做。这是你膝盖上的一枪

以下解决方案创建一个表和一个结构。
该表需要一些填充,因为所有列的长度必须相同(并且 4195 不是 120 的倍数)。

代码有点复杂,我尝试在不使用 for 循环的情况下解决它(为了使解决方案更高效[更有趣])。
我希望你能遵循代码,并从中学习......

这是一个代码示例(解释在评论中):

% Fill z1 with sequential numbers (just for testing)
z1 = (1:4195)';

z1_len = length(z1);

% Compute length remainder from 120
len_mod120 = mod(z1_len, 120);
pad_len = mod(120 - len_mod120, 120);

% If length of z1 is not a multiple of 120, add zeros padding at the end.
pad_z1 = padarray(z1, pad_len, 0, 'post'); % After padding, length of z1 is 4120

% Reshape pad_z1 into a matrix where each row is 120 elements
% Z1(:, 1) gets z1(1:120), Z1(:, 2) gets z1(121:240)...
Z1 = reshape(pad_z1, [], length(pad_z1)/120);

% Build naming indices
name_idx = zeros(1, 2*length(pad_z1)/120);
name_idx(1:2:end) = 1:120:length(pad_z1);       %First naming index:  1   121  241  361 ...
name_idx(2:2:end) = name_idx(1:2:end) + 120-1;  %Second naming index: 120 240  360  480

% String of elements names separated by space
str_names = sprintf('z1_%d_%d ', name_idx); % 'z1_1_120 z1_121_240 z1_241_360 z1_361_480 ...

% Build cell array of names
var_names = split(str_names(1:end-1)); %{'z1_1_120'}, {'z1_121_240'}, {'z1_121_240'}

% Build table, where each column is 120 elements, and column names are 'z1_1_120' 'z1_121_240' 'z1_121_240'
% A table is useful, if you don't care about the zero padding we added at the beginning
% https://www.mathworks.com/matlabcentral/answers/376985-how-to-convert-string-to-variable-name
T = array2table(Z1, 'VariableNames', var_names);

% Convert table to struct:
% S.z1_1_120 holds first 120 elements, S.z1_121_240 holds next 120 elements...
S = table2struct(T, 'ToScalar', true);

% Fix the last field in the stract - should contain only 115 elements (not 120)
S = rmfield(S, var_names{end}); % Remove the last field from the struct

% last_field_name = 'z1_4081_4195'
last_field_name = sprintf('z1_%d_%d', name_idx(end-1), z1_len);

% Add the last field (only 195 elemtns)
S.(last_field_name) = z1(end-len_mod120+1:end);

药片:

T =

  120×35 table

    z1_1_120    z1_121_240    z1_241_360    z1_361_480    ...
    ________    __________    __________    __________    

        1          121           241           361           
        2          122           242           362           
        3          123           243           363           
        4          124           244           364           
        5          125           245           365           
        6          126           246           366           
        ...        ...           ...           ...

访问第一个元素的示例:T.z1_1_120(1)


结构 S:

S = 

  struct with fields:

        z1_1_120: [120×1 double]
      z1_121_240: [120×1 double]
      z1_241_360: [120×1 double]
      z1_361_480: [120×1 double]
      z1_481_600: [120×1 double]
      z1_601_720: [120×1 double]
      z1_721_840: [120×1 double]
      ...
      z1_4081_4195: [115×1 double]

访问第一个元素的示例:S.z1_1_120(1)


推荐阅读