0
votes

I have a cell array (C) containing 5 matrices. Each matrix represents different set of vectors (i.e. Each matrix has two columns. One is x coordinate; the other is y coordinate. The row number varying depends on the number of vectors)

C{1} = [20x2 double];
C{2} = [23x2 double]; 
C{3} = [32x2 double] ...

In this case, there are 20 vectors in C{1}; 23 vectors in C{2} and so on. Is there any way (other than one or two for loop)to do the dot product for the two adjacent vectors for each matrix?

C{1} = [2,3; 1,2; 5,4; 8,3; ...]  

calculate the dot product for [2,3]&[1,2] then [1,2]&[5,4] then [5,4]&[8,3] and so on.

So at the end, I would expect to get a cell array with 5 cells. Each cell is an [n-1,1] array (n is the length of the matrix).

dots = [5x1 cell]. 
dots{1} = [19x1 double]; 
dots{2} = [22x1 double];
dots{3} = [31x1 double] ...
1

1 Answers

0
votes

You can use cellfun to compute the dot product between each coordinate (row) and the next coordinate (row).

dots = cellfun(@(x)dot(x(1:end-1,:), x(2:end, :), 2), C, 'uniform', 0)

Explanation

We grab the first rows to compare using x(1:end-1,:) and then want to perform the dot product with the next row x(2:end,:). When performing the dot product, we want to specify that we want the dot product along the second dimension so we provide a 2 for the third input to dot.

dp = dot(C{1}(1:end-1,:), C{1}(2:end,:), 2);

We use cellfun to perform this on each cell array element.

Example

C = {rand(10,2), rand(20,2), rand(30,2), rand(40, 2), rand(50,2)};
dots = cellfun(@(x)dot(x(1:end-1,:), x(2:end, :), 2), C, 'uniform', 0)

    [9x1 double]   [19x1 double]   [29x1 double]   [39x1 double]   [49x1 double]