在MATLAB中保存变量的值

时间:2016-09-07 00:45:25

标签: matlab loops variables

您好我的代码我想知道如何最好地保存我的变量columncolumn是733x1。理想情况下我想拥有 column1(y)=column,但我收到错误:

  

无法从逻辑转换为单元格。

在内循环中。我发现很难在overlap中访问这些存储的值。

for i = 1:7 
    for y = 1:ydim % ydim = 436
        %execute code  %code produces different 'column' on each iteration 
        column1{y} = column; %'column' size 733x1 %altogether 436 sets of 'column'
    end
    overlap{i} = column1; %iterates 7 times. 
end

理想情况下,我希望overlap存储7个保存的变量(733x436) 感谢。

1 个答案:

答案 0 :(得分:1)

我假设column是使用一个程序计算的,其中每列依赖于后者。如果没有,那么很可能会对此做出改进:

column = zeros(733, 1);           % Might not need this. Depends on you code.
all_columns = zeros(xdim, ydim);  % Pre-allocate memory (always do this)
                                  % Note that the first dimension is usually called x,
                                  % and the second called y in MATLAB
overlap = cell(7, 1);
overlap(:) = {zeros(xdim, ydim)}; % Pre-allocate memory

for ii = 1:numel(overlap)         % numel is better than length
    for jj = 1:ydim               % ii and jj are better than i and j
        % several_lines_of_code_to_calculate_column
        column = something;
        all_columns(:, jj) = column;
    end
    overlap{ii} = all_columns;
end

您可以像overlap一样访问overlap{1}(1,1);中的变量:overlap{2}。这将获得第一个单元格中的第一个元素。 var1将获得第二个单元格中的整个矩阵。

您指定了7个变量。您的代码暗示您知道单元格比将其分配给不同的变量(var2column = zeros(733, 1); % Might not need this. Depends on you code. overlap = zeros(xdim, ydim, 7) % Pre-allocate memory for 3D-matrix for ii = 1:7 for jj = 1:ydim % several_lines_of_code_to_calculate_column column = something; all_column(:, jj, ii) = column; end end ...)更好。好!具有不同变量的解决方案是糟糕的坏事。

您可以改为使用3D数组,而不是使用单元格数组。如果您可以对内容进行矢量化,这可能会在以后更快地进行处理。

这将是:

PUT <server>/odata/v2/Candidate('myId')
data: 
{"custAnrede" : {"id":"555"}}
}