I have an a
-by-b
cell array, C
. In each element, there is a float array.
I now want to create a new symmetric matrix M
. Each element (i, j)
in M
is to be set to the sum of the Euclidean distances of all the respective float arrays in C
.
For example, to find M(i,j)
, I would take the set of b
float arrays in C
along row i
, and the set of b
float arrays in C
along row j
, find the Euclidean distance between each array across the two sets, and then sum up the b x b
values. C{i,j}
is a column vector. All columns are the same length.
Below is my "brute force" implementation of this:
for i=1:a
for j=1:a
dist_sum = 0;
for k=1:b
for l=1:b
dist = sqrt(sum((C{i, k} - C{j, l}) .^ 2));
dist_sum = dist_sum + dist;
end
end
M(j, i) = dist_sum;
M(i, j) = dist_sum;
end
end
My question: Is there a more efficient way of doing this using matrix operations, without having to explicitly compute each Euclidean distance in turn?
C{i,j}
? A row vector? With the same length for alli
andj
? – Luis MendoC{i,j}
is a column vector. All columns are the same length. – Karnivaurusk
is each vector component. Is that possible for you? – Luis Mendocell2mat
? That should make it easier to use functions likepdist
. – Cecilia