Matlab generate variable names when subdividing large data [duplicate]

二次信任 提交于 2021-02-11 15:53:47

问题


I have a large data set (vector) I want to split up in to n smaller sets to look at later with other scripts. I.e.if n = 10 I want to turn one 1x80000000 double in to ten 1x8000000 doubles. My thoughts are turn the original in to a n by m matrix then save each row of the matrix in to it's own vector, as follows.

%data-n-splitter
n = 10 %number of sections
L = length(data);
Ls = L/n;
Ls = floor(Ls);
Counter = 1;

%converting vector to matrix
datamatrix = zeros(n,Ls);
for k = 1:n
 datamatrix(k,:) = data(Counter:Counter+ Ls - 1);
 Counter = Counter + Ls;
end

How do I make matlab loop this part of the code n times:

%save each row of matrix as seperate vector

P1 = datamatrix(1,:);
P2 = datamatrix(2,:);
P3 = datamatrix(3,:);
P4 = datamatrix(4,:);
P5 = datamatrix(5,:);
P6 = datamatrix(6,:);
P7 = datamatrix(7,:);
P8 = datamatrix(8,:);
P9 = datamatrix(9,:);
P10 = datamatrix(10,:);

Example answer that I'm hoping for:

for k = 1:n
P('n') = datamatrix(n,:);
end

I've seen some articles about using cell arrays but the scripts I'm passing the variables to aren't set up for this so I'd rather not go down that route if possible.


回答1:


There are several options:

  • use a struct, which comes closest to what you are hoping for,
  • use a cell, more convenient looping but no access over meaningful names,
  • use a higher-dimension matrix (in your case it is only 2D, but the same applies for 3D or higher). This is the most memory-efficient option.
  • To round this off, you could also use a table, which is a hybrid of a struct and a cell as you can use both notations to access it. There is no other benefit.

Now, how to do this? The simplest (and best) solution first: create a 2D matrix with reshape

Ary = 1:10; % I shrank your 1x80000000 array to 1x10 but you'll get the idea
%% create new structure
Mat = reshape(Ary,5,2);
%% access new structure (looping over columns)
for i = 1:size(Ary,2)
   % access columns through slicing
   ary_sct = Mat(:,i);
   % do something
end

Pro: memory efficient (requires the same amount of memory as the initial array); easy looping

Con: only works if you can slice the initial array evenly

Next: create a cell

Ary = 1:10;
n = 2; % number of sections
L = floor(length(Ary)/n);
% allocate memory
C = cell(1,n);
%% create new structure
for i = 1:n
    % access the content of a cell with {}
    C{i} = Ary((i-1)*L+1:i*L);
end
%% access new structure (looping over entries)
for i = 1:length(C)
    % access the content of a cell with {}
    ary_sct = C{i};
    % do something
end

Pro: You can store anything in a cell. Every data type and -- what is often more important -- of any dimension

Con: The accessing the content (through {}) or accessing the element (through ()) is a bit annoying if your are a beginner; each element require a memory overhead of about 60 bytes as those are pointers, which need to store the information where and on what they are pointing.

Next: use a struct

Ary = 1:10;
n = 2; % number of sections
L = floor(length(Ary)/n);
% create empty struct
S = struct();
%% create new structure
for i = 1:n
    % create fieldname (must start with a character!)
    fld = num2str(i,'F%d');
    % write to field (note the brackets)
    S.(fld) = Ary((i-1)*L+1:i*L);
end
%% access new structure (looping over fieldnames)
% get all field names
FlNms = fieldnames(S);
for i = 1:length(FldNames)
    % access field names (this is a cell!)
    fld = FldNms{i};
    % access struct
    ary_sct = S.(fld);
    % do something
end

Pro: Field names are convenient to keep the overview of your data

Con: accessing field names in a loop is a bit tedious; each element require a memory overhead of about 60 bytes as those are pointers, which need to store the information where and on what they are pointing.



来源:https://stackoverflow.com/questions/61227946/matlab-generate-variable-names-when-subdividing-large-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!