0

Hi for my code I would like to know how to best save my variable column. column is 733x1. Ideally I would like to have column1(y)=column, but I obtain the error:

Conversion to cell from logical is not possible.

in the inner loop. I find it difficult to access these stored values in 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

Ideally I want overlap to store 7 variables saved that are (733x436).
Thanks.

4
  • To rephrase your question, you are asking, how to access the stored values in overlap, is that what you are asking? Commented Sep 7, 2016 at 1:08
  • Is there any particular reason you're using cell arrays rather than concatenating your column vectors into a 2d array? It appears that they're all the same size. Commented Sep 7, 2016 at 1:26
  • There are numerous conversion from cells to matrices in MATLAB. Since we do not know the size of column_fill or how it relatese to column1 it is hard to say though. But maybe this is not the problem? I just assumed this since you provided an error message. Commented Sep 7, 2016 at 7:20
  • Thank you for pointing out that mistake in my post. Now updated. Commented Sep 7, 2016 at 20:14

1 Answer 1

1

I'm assuming column is calculated using a procedure where each column is dependent on the latter. If not, then there are very likely improvements that can be made to this:

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

You can access the variables in overlap like this: overlap{1}(1,1);. This will get the first element in the first cell. overlap{2} will get the entire matrix in the second cell.

You specified that you wanted 7 variables. Your code implies that you know that cells are better than assigning it to different variables (var1, var2 ...). Good! The solution with different variables is bad bad bad.

Instead of using a cell array, you could instead use a 3D-array. This might make processing later on faster, if you can vectorize stuff for instance.

This will be:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.