0
votes

I am having an issue when reshaping a cell array:

w = size(im,1);                                % width size
h = size(im,2);
d = size(crossfield,3);
for pxRow = 1:h % fixed pixel row
  for pxCol = 1:w % fixed pixel column
    for pxBreadth = 1:d    
      for r = 1:h % row of distant pixel
        for c = 1:w % column of distant pixel
          for z = 1:d
 
            field(c,r,z) = crossfield(c,r,z).*rmatrix(c,r,z);                
 
          end
        end
      end
    b(i) = {field}; % filling a cell array with results. read below
    i = i+1;
    end
  end
end
 
 b = reshape(b, w, h,z);

and the error:

Error using ==> reshape

To RESHAPE the number of elements must not change.

some other info which may be of use:

>> size(im)

ans =

    35    35

>> size(crossfield)

ans =

    35    35     3

>> size(rmatrix)

ans =

    35    35     3
>> size(w)

ans =

     1     1

How am I able to reshape b?

1
Your code runs fine on my machine (no error when I call reshape). What is the initial value of i? And what do you see when you do size(b) after running the loop? I set i=1; before the loop, and see b as being 1x3675 after running the loop.Chris Taylor

1 Answers

1
votes

Note that your three inner loops have the same effect as doing

field = crossfield .* rmatrix;

and your three outer loops just set every element of the cell array b to the same value. Therefore your code can be simplified to:

[w h] = size(im);
d = size(crossfield,3);

b = cell(w,h,d);
b(:,:,:) = {crossfield .* rmatrix};