matlab - to find mean square error of two cell arrays of different sizes -
i have 2 cell arrays. 1 'trans_blk' of size <232324x1> consists of cells of size <8x8> , 'ca' of size <1024x1> consists of cells of size <8x8>. want compute mean square error (mse) each cell of 'ca' respect every cell of 'trans_blk'.
i used following code compute:
m=0; ii=0:7 jj=0:7 m=m+((trans_blk{:,1}(ii,jj)-ca{:,1}(ii,jj))^2); end end m=m/(size of cell); //size of cell=8*8 disp('mse=',m);
its giving error. bad cell reference operation in matlab.
a couple of ways figured go:
% first define mse function mse = @(x,y) sum(sum((x-y).^2))./numel(x);
i'm big fan of using bsxfun
things this, unfortunately doesn't operate on cell arrays. so, borrowed singleton expansion form of answer here.
% singleton expansion way: mask = bsxfun(@or, true(size(a)), true(size(b))'); idx_a = bsxfun(@times, mask, reshape(1:numel(a), size(a))); idx_b = bsxfun(@times, mask, reshape(1:numel(b), size(b))'); func = @(x,y) cellfun(@(a,b) mse(a,b),x,y); c = func(a(idx_a), b(idx_b));
now, if bit crazy (or if explicitly making arrays a(idx_a)
big), try loop approach such 1 below.
% or quick loop: results = zeros(length(a),length(b)); y = b{1}; iter = 1:length(b) y = b{iter}; results(:,iter) = cellfun(@(x) mse(x,y) ,a); end
if run out of memory: think of allocating: matrix of doubles (232324 x 1024) elements. (that's decent chunk of memory. depending on system, close 2gb of memory...)
if can't hold in memory, might have decide going mse's , either in batches, or find machine can run full simulation/code on.
edit if want keep sum of mses (as op states in comments below), can save on memory by
% sum go along: results = zeros(length(a),1); y = b{1}; iter = 1:length(b) y = b{iter}; results = results + cellfun(@(x) mse(x,y) ,a); end results =sum (results);
Comments
Post a Comment