2016-09-07 88 views
0

嗨,我的代码我想知道如何最好地保存我的变量columncolumn是733x1。我非常希望有 column1(y)=column,但我得到的错误:在MATLAB中保存变量的值

Conversion to cell from logical is not possible.

在内环

。我发现很难访问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)。
谢谢。

+0

重组你的问题,你问,***如何访问'overlap' ***存储的值,是你问什么? –

+0

是否有任何特别的原因,你使用单元格数组而不是串联你的列向量到2d数组?看起来它们都是相同的大小。 – beaker

+0

MATLAB中有很多从单元格到矩阵的转换。由于我们不知道'column_fill'的大小或它与'column1'的关系,所以很难说。但也许这不是问题?自从您提供了一条错误消息之后,我只是假设这一点。 – patrik

回答

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}将获得第二个单元格中的整个矩阵。

你指定你想要7个变量。您的代码意味着您知道单元格比将其分配给不同的变量(var1var2 ...)要好。好!具有不同变量的解决方案是坏坏坏。

除了使用单元阵列,您可以改为使用3D阵列。这可能会使得处理速度更快,例如,如果可以将东西矢量化。

这将是:

column = 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 
+0

非常感谢! –