连续标记数据块

问题描述 投票:0回答:1

我在4195X1中包含的数据被称为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);
matlab for-loop extract
1个回答
0
投票

正如丹尼尔评论,不要这样做!

如果需要描述的标签类型,则可以使用Table或Struct数据类型。

读取following post

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

以下解决方案创建一个表和一个结构。该表需要一些填充,因为所有列的长度必须相同(并且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:

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 Struct 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)

© www.soinside.com 2019 - 2024. All rights reserved.