user294448
user294448

Reputation: 5

for loop, cell summation

I'm trying to perform a summation. omega is a scaler and eigenVecs are vectors, there are i (19) of them. I want to sum them into 1 vector. I want to store that sumations in faceRecon{1}, the next in faceRecon{2} and so on.

for i =1:N/PicDup;
  for j =1:k;
    faceRecon{i} += mtimes(omega{i,j},eigenVecs{i});
  end;
end;
%N/PicDup = 7; k = 35; 

I get this error: error: operator +: nonconformant arguments (op1 is 0x0, op2 is 11088x1);

I kind of understand the error, I need to initialize op1 a cell array that is {1x7} and inside each one of those cells in need to make them 11088x1.

How do I do this? Is this even possible? Is there a better cleaner way to do this?

Upvotes: 0

Views: 51

Answers (1)

Suever
Suever

Reputation: 65430

To fix your immediate issue, you need to initialize faceRecon such that each element contains an 11088 x 1 matrix of zeros prior to iterating through the loop.

faceRecon = repmat({zeros(size(eigenVecs{1}))}, size(eigenVecs));

Also, += is not a MATLAB operator so you'll need to do something like the following instead

thing = thing + other_thing;

Rather than using cell arrays, I would convert omega and eigenVecs to actual numeric matrices and then you can use normal matrix multiplication to perform the multiplication and subsequent summation.

w = cell2mat(omega);            % M x N
eigenV = cat(2, eigenVecs{:});  % 11088 X M;

faceRecon = eigenV * w;         % 11088 x N

Upvotes: 2

Related Questions