0

I have 3x3 cells a in Matlab, each cell containing a vector of 9x1

a=cell(3,3);
for i=1:3
    for j=1:3
        a{i,j}=randi(10,[9 1]);
    end
end

I would like to multiply each cell of a by its transpose and then sum across cells but I don't know how to apply these operations to cells.

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
TEX
  • 2,249
  • 20
  • 43
  • I assume that you are talking about the multiplication that results in a single value (i.e. scalar product of the vector with itself, `row * col`). In this case, the value will be the sum of the squared elements. This is what you should be asking for... It can be achieved for example by `reshape(sum([a{:}].^2),size(a))`. – Dev-iL Aug 14 '14 at 09:56
  • Each cell of a is a column vector and I want to multiply it by its transpose, hence col*row. – TEX Aug 14 '14 at 09:58
  • This is something you should mention - multiplication by transpose works both ways for vectors! So you want to get the sum of the elements of the symmetric `9x9` matrices that results from `col * row` of each cell, correct? And do you want the output to be a single value, or a matrix, or a cell array, or ....? – Dev-iL Aug 14 '14 at 10:01
  • The sum of the symmetric 9x9 matrices that results from col * row of each cell will be a 9x9 matrix. – TEX Aug 14 '14 at 10:09

2 Answers2

2

I think this does what you want:

M = cellfun(@(v) v*v.', a, 'uni', 0); %'// each vector times its transpose
M = cat(3, M{:}); %// concat along 3rd dim
result = sum(M, 3); %// sum along 3rd dim
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
0

Not sure what you are trying to accomplish here, but if you would like to multiply each cell by its transpose and get the sum of all the elements in that cell you can use something like:

clear all, close all
a=cell(3,3);
b=cell(3,3);
for i=1:3
    for j=1:3
        a{i,j}=randi(10,[9 1]);
        a{i,j} = a{i,j} * a{i,j}.'; % multiply by transpose
        b{i,j} = sum(sum(a{i,j}))
    end
end
Scott
  • 99
  • 5