How to find the difference in values present in each row of two cell arrays of same size.

4 views (last 30 days)
I am having two cell arrays A and B of same size as follows
A B
1 1
1 1
1 1
1 1
1 1
[1,2] [1,2]
[1,1] [1,2]
[1,2] [1,1]
[1,2] [1,2]
[1,1] [1,2]
[1,2,2] [1,1,2]
[1,2,3] [1,2,2]
[1,2,3] [1,2,2]
[1,1,2] [1,1,2]
[1,2,2] [1,2,3]
I want to find the difference between A and B.
As the size is small I can do it manually by seeing it.
On seeing it I can find 7th ,8th, 10th, 11th, 12th, 13th, 15th rows are different.
But I having cell arrays for larger size and I am unable to do it manually.
Could anyone please help how to find the rows in a general way that are different using matlab command.

Accepted Answer

Adam Danz
Adam Danz on 24 Jun 2021
Edited: Adam Danz on 24 Jun 2021
I want to find the difference between A and B.
Set up a loop that runs isequal(A,B) on pairs from cell array A and B.
A = {1; [1 2]; [1 1]; [1 2 2]};
B = {1; [1 1]; [1 1]; 1 };
iseq = false(size(A));
for i = 1:numel(A)
iseq(i) = isequal(A{i},B{i});
end
iseq
iseq = 4×1 logical array
1 0 1 0
Or use cellfun that performs the loop above within 1 line of code,
iseq = cellfun(@isequal, A,B)
iseq = 4×1 logical array
1 0 1 0
These methods require A and B to be equal lengths.
I want to calculate the mean square error of the two cell arrays.
The pairs of vectors in A and B must have the same length.
A = {1; [1 2]; [1 1]; [1 2 2]};
B = {1; [1 1]; [1 1]; [1 2 3]};
mse = nan(size(A));
for i = 1:numel(A)
mse(i) = sum((A{i}-B{i}).^2)/numel(A{i});
end
mse
mse = 4×1
0 0.5000 0 0.3333
Or use cellfun that performs the loop above within 1 line of code,
mse = cellfun(@(A,B)sum((A-B).^2)/numel(A), A,B)
mse = 4×1
0 0.5000 0 0.3333
  4 Comments
Adam Danz
Adam Danz on 24 Jun 2021
To count the number of paired coordinates that are 'near',
sum(iseq)
You can compute the MSE between the pairs of vectors within the loop or you could do it within cellfun. However, the paired vectors must always have the same number of values within each pair.
I can update my answer to show how.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!