matlab - Squared euclidean calculation with cell arrays -


i start university project in few months , planned write in java tutor recommended me pick matlab lot easier , efficient, i'm writing few proof of concept prototypes 'test waters'.

right have cell array , want find total sum of squares elements in (cell) array. in other words, need go through each element, find mean, calculate distance each point in element mean, total each element , return single figure entire array.

this how approached it:

function squared_sum = sumsquares(c_array) squared_sum = 0 = 1:size(c_array,2)     c_element = cell2mat(c_array(1,i));     j = 1:size(c_element,1)         square_distance = pdist2(c_element(j,:), mean(c_array{1,i})).^2; % sum of squares = euclidean distance squared         squared_sum = squared_sum + square_distance;     end end end 

the issue how write in java. i'm aware functional paradigm supposed more elegant , less reliant on loops i'm curious if there nicer way write this?

edit: cell structure (1x4) [28x3] [42x3] [8x3] [91x3]

for regular shaped cell arrays ( cells of same size), use bsxfun -

c_array3d = cat(3,c_array{:}); sqdiffs = bsxfun(@minus,c_array3d,mean(c_array3d,1)).^2; squared_sum = sum(sqdiffs(:)); 

for irregular shaped cell arrays, reduce loop complexity using pdist2 work on 2d arrays instead of 1d arrays, -

squared_sum = 0; = 1:size(c_array,2)     squared_sum = squared_sum + sum(pdist2(c_array{1,i}, mean(c_array{1,i})).^2); end 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -