could anyone help me how to compute the mean square error between two cell.

I am having two cell A and B in the attached data.mat.
I want to compute the mean square error between A and B in data.mat.
Could anyone please help me on this.

2 Comments

How are you defining the MSE for your data? Each cell of A and B is a N-by-2 vector.
Here, both A and B has the same N-by-2 vector, for example
A{1,1}
1 2
1 1
1 1
1 1
1 2
1 1
1 2
1 2
1 2
1 2
B{1,1}
1 1
1 2
1 1
1 1
1 1
1 1
1 2
1 2
1 2
1 1
So first column of A needs to get subtracted with first column of B denoted by C and second column of A needs to get subtracted with second column of B denoted by D. Then I need to find the difference between C and D, square it and add all the rows and divide it by the 10.

Sign in to comment.

 Accepted Answer

cellfun(@(a,b) sqrt(sum(a(:)-b(:)).^2))
You had the wrong definition of mean square error.

10 Comments

When i used the command
cellfun(@(a,b) sqrt(sum(a(:)-b(:)).^2))
i am getting error stating not enough input arguments.
I am getting error stating Undefined operator '-' for input arguments of type 'cell'.
What Walter provided should work. Again, that's the wrong definition of MSE.
unless, that is, what you have is a cell array containing cell arrays, instead of a cell array containing double. Cell array containing cell array was where you seemed to be headed in an earlier question that I was helping you with but which is stalled waiting for your reply about exactly what output you would want for the sample array I posted.
with respect to the example shown
A{1,1}
1 2
1 1
1 1
1 1
1 2
1 1
1 2
1 2
1 2
1 2
B{1,1}
1 1
1 2
1 1
1 1
1 1
1 1
1 2
1 2
1 2
1 1
here first column of A needs to be subtracted with first column of B and second column of A needs to be subtracted with second column of B.The same process needs to repeated for A{2,1} with B {2,1}, A{3,1} with B{3,1}, A{4,1} with B{4,1} and A{5,1} with B{5,1). Now the first column needs to be subtracted with second column among themselves and sum all the columns divided by the total number of rows. this needs to be repeated for {2,1} {3,1} {4,1} and {5,1}.Then finally all needs to be sum up and divide by the total number of rows.
If my explanation is not clear please let me know.
A{1,1} = [1 2
1 1
1 1
1 1
1 2
1 1
1 2
1 2
1 2
1 2];
B{1,1} = [1 1
1 2
1 1
1 1
1 1
1 1
1 2
1 2
1 2
1 1];
cellfun(@(a,b) (sum(a(:)-b(:)).^2)/numel(a), A, B)
ans = 0.2000
here first column of A needs to be subtracted with first column of B and second column of A needs to be subtracted with second column of B.The same process needs to repeated for A{2,1} with B {2,1}, A{3,1} with B{3,1}, A{4,1} with B{4,1} and A{5,1} with B{5,1).
The cellfun code I posted does that.
Now the first column needs to be subtracted with second column among themselves
I am not sure which first column is to be subtracted with which second column.
If you are trying to find the mean-squared-error comparing corresponding locations in A and B, then you do not subtract between different columns. Mean squared error of P and Q is sum((P-Q).^2,'all')/numel(P)
i am getting error stating Undefined operator '-' for input arguments of type 'cell'.
Please show the output of the following:
cellfun(@(a)string(class(a)), A)
cellfun(@(a)string(class(a)), B)
I suspect your A and B are not cell array of double like you indicate here. Judging by some of your other posts, I suspect that you have a mix, that some of the entries are double and others are cell.

Sign in to comment.

More Answers (1)

Anyhow i managed to get the partial output as required using the following commands for my data.mat as attached.
C=cellfun(@minus,A,B,'UniformOutput',false);
D=cellfun(@(x) sum((x(:,1)-x(:,2)).^2)/10,C,'UniformOutput',false)
Now I want to sum up all the rows of D and divide it by 5.
Could you please help me on this.

5 Comments

However, this has no relationship to mean squared error.
Ok, I can understand. Suppose if I need to calculate the mean square error for the data.mat could you help me how it can be done.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!