to find mean square error of two cell arrays of different sizes

I have two cell arrays. One is 'trans_blk' of size <232324x1> consists of cells of size <8x8> and another 'ca' is of size <1024x1> consists of cells of size <8x8>. I want to compute mean square error (MSE) for each cell of 'ca' with respect to every cell of 'trans_blk'.

I used the following code to compute:

m=0; for ii=0:7 for 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 an error. Bad cell reference operation in MATLAB.

-------------Problems Reply------------

A couple of ways that I figured you could go:

% First define the MSE function
mse = @(x,y) sum(sum((x-y).^2))./numel(x);

I'm a big fan of using bsxfun for things like this, but unfortunately it doesn't operate on cell arrays. So, I borrowed the singleton expansion form of the answer from 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 that is a bit too crazy (or if explicitly making the arrays by A(idx_A) is too big), then you could always try a loop approach such as the one below.

% Or a quick loop:
results = zeros(length(A),length(B));
y = B{1};
for iter = 1:length(B)
y = B{iter};
results(:,iter) = cellfun(@(x) mse(x,y) ,A);
end

If you run out of memory: Think of what you are allocating: a matrix of doubles that is (232324 x 1024) elements. (That's a decent chunk of memory. Depending on your system, that could be close to 2GB of memory...)

If you can't hold it all in memory, then you might have to decide what you are going to do with all the MSE's and either do it in batches, or find a machine that you can run the full simulation/code on.

EDIT If you only want to keep the sum of all the MSEs (as OP states in comments below), then you can save on memory by

% Sum it as you go along:
results = zeros(length(A),1);
y = B{1};
for iter = 1:length(B)
y = B{iter};
results = results + cellfun(@(x) mse(x,y) ,A);
end
results =sum (results);

Category:matlab Views:0 Time:2018-05-15

Related post

  • Compare two int arrays of different size in O(N * log(N)) time? 2012-01-03

    I need to compare between two int arrays of different size. It has to be most efficient. Is it possible to do in O(N * log(N)) time? It should print integers that differ between the two arrays. --------------Solutions------------- The algorithm is O(

  • How to find the mean square error in matlab 2010-09-11

    Is there a way to find the mean square error in matlab between 2 images A,B(say) in true color of dimension 256*256*3 ? The mathematical formula for a matrix say M1 and M2 is as under mean sq err=1/n*n { summation (square[M1(i,j)-M2(i,j)])} where i s

  • Root mean square difference between two images using Python and PIL 2010-06-23

    I need to have a function like the one found here: http://effbot.org/zone/pil-comparing-images.htm that calculates the root mean square difference between two images. The code looks like this: import ImageChops import math, operator def rmsdiff(im1,

  • Reducing Mean Square Error 2010-02-28

    The topic of mine is face recognition using artificial neural networks using MATLAB code. I tried my work by taking the images from web and started implementing the program by using neural networks. By typing nprtool command in MATLAB ,i started trai

  • Passing two-dimensional arrays of varying size in c++ 2012-02-28

    I'm trying to call a function with a two-dimensional array of variable size as a parameter. In order to do this, I use a template: template <int N, int M> void move(int (&arr)[M][N]); Now this works fine when I declare the size of the array

  • How to merge two data frames with different sizes using R 2011-10-25

    I am working on two data frames with different sizes. matA : col1 col2 col3 row1 aa abc 123 row2 cc dfg 455 row3 ..ee efg 345 matB : col1 col4 col5 row1 aa a1 b1 row2 cc a2 b2 row3 dd a3 b3 row4 ee a4 b4 dim(matA) : 2000 * 3 dim(matB) : 4000 * 3 matC

  • Find similar rows in 3 two-dimensional arrays 2012-01-09

    What is the best approach to solve the following problem in Java? There are 3 two-dimensional arrays that have equal number of rows and columns: Array1: [1]: {1, 2, 3} [2]: {2, 2, 4} [3]: {1, 1, 1} Array2: [1]: {1, 2, 0} [2]: {1, 2, 3} [3]: {1, 0, 1}

  • Find occurrences of distinct values in cell array in Matlab 2012-02-10

    I have a data file of two columns ('date' and 'user'): date1 user1 date1 user1 date1 user2 date2 user1 date2 user2 ... I need to find how many times each unique user did the action at the certain date. I know I could use unique() function to find the

  • Finding common elements in two arrays of different size 2010-12-25

    I have a problem to find common elements in two arrays and that's of different size. Take , Array A1 of size n and Array A2 of size m, and m != n So far, I've tried to iterate lists one by one and copy elements to another list. If the element already

  • find the longest intersection of two integer arrays 2011-10-19

    I have this problem but don't know what's best way to solve it in term of time complexity and space complexity. Suppose I have two integer arrays a={1,2,3,4,5} b={2,3,4,5,6} and of course they not necessarily to be sorted. So the question is how to f

  • Difference of two cell arrays of strings in Matlab 2012-01-15

    I have a cell array like a={'potential'; 'impact'; 'of'; 'stranded'; 'assets'; 'and'; 'other'; 'necessary'; 'regulatory'; 'approvals'; 'assets'} and want to subtract from it an array like b={'a'; 'and'; 'of'; 'my'; '#'}. Using setdiff(a,b) sorts my a

  • How to calculate residuals for two curves (matrixes) of different size? 2012-01-27

    I've got a theoretical curve which was calculated numerically and an experimental curve (better to say a massive of experimental points). I need to calculate the residuals between these two curves to check the accuracy of modeling with the least squa

  • Compilation error: cannot allocate an array of constant size 0. Why do I get this? 2009-05-26

    I faced one issue while working on a C code with Microsoft Visual Studio-2005 compiler. I tried to declare a big buffer statically as : int gbl_data[4096*4096*256]; EDIT: This declaration was a global variable in a header file. It was giving an compi

  • How to pass a two dimensional array of unknown size as method argument 2011-05-06

    I'm trying to pass a two-dimensional array, which size can be dynamic, as a method argument. Within the method I'd like to use the array with the general array syntax. int item = array[row][column]; To pass the array is not possible, so I thought abo

  • Two-dimensional array of different types 2012-04-08

    I want to create a two-dimensional array in which I want to store records from the database. So lets say that the first is of type int and the second of type String (here I am describing just one record so basically types of db columns). How can I do

  • comparing of two 2d-string arrays with different sizes 2012-04-19

    I have the following 2-dimensional Arrays: String[][] array1 = {{"hello", "hi"}{"bye", "two"}}; String[][] array2 = {{"hello", "hi", "three"}{"bye", "maybe", "third"}, {"3", "rd", "Element"}}; String[][] array3 = {{"hello", "hi"}{"bye", "two"}}; How

  • Logical Error when calculating mean square error 2011-03-21

    a=imread('pic1.jpg'); b=0.25*a; c=4.0*b; figure; imshow(c); MSE = reshape(mean(mean(((a) - (c)).^2,2),1),[1,3]) Code works fine without any errors. Size of a is 256*256*3 RGB type. However, There are 2 issues : Now logically, the MSE should have been

  • MATLAB: fastest way to do a root-mean-squared error between a vector and array of vectors 2011-11-29

    I have a question regarding the fastest way to compute the RMSE between a single vector and an array of vectors. Specifically, I have a vector A representing an point and would like to find the index in a list B of points that A is closest to. Right

  • Calculating MSE (Mean Squared Error) 2012-03-01

    I'm not sure if this is the right place to ask this, but where can a find a step-by-step guide on how to compute the MSE of two images? I know what the formula is but I have no idea how to put it into practice. --------------Solutions------------- In

  • matlab neural network gradient descent and mean square error 2011-05-12

    I want to know how grdient descent algorithm works on matlab network training and how MSE is calculated - I have my own app but it doesnt work as the matlab nn and I want to know why. My algorithm looks like this: foreach epoch gradient_vector = 0 //

  • Distance measure between two sets of possibly different size 2010-12-14

    I have 2 sets of integers, A and B, not necessarily of the same size. For my needs, I take the distance between each 2 elements a and b (integers) to be just abs(a-b). I am defining the distance between the two sets as follows: If the sets are of the

  • How to align two divs that have different sizes horizontally 2012-01-02

    Suppose I have two divs: <div id="container"> <div id="left">line one</div> <div id="right">line one<br/>line two</div> </div> How to make left div and right div align on the bottom line? Basically to expand

  • Two Hard-coded Arrays OF DIFFERENT SIZES into One 2-Dimensional Vector 2012-03-20

    I realize that if I want "valArray" to populate to the right of "COLUMN 0,0" I should put "[j][1]", however, I keep getting an error when I do that. OUTPUT:----------------------------------------------------------------------------------------------

  • How do you add two int arrays with different lengths? 2013-12-18

    I've been working on a Java lab that wants us to have the user enter two digits up to 50 digits long and add them together. I've successfully been able to complete everything except for when the two arrays have a different length. I've been toying ar

  • Merging two images in php (different sizes) 2011-12-16

    I've found this script to merge two images (keeping transparency) in php: function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct, $trans = NULL) { $dst_w = imagesx($dst_im); $dst_h = imagesy($dst_im); //

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.083 (s). 11 q(s)