MATLAB Answers

Get all unique combinations from cell array for use as functional arguments

20 views (last 30 days)
I have data stored in a n x n cell array. For example:
data = {rand(1000,1) rand(1000,1); rand(1000,1) rand(1000,1)};
I want to compute the mean squared error between all possible unique cell combinations. In this case, if computed manually, they would be the following:
immse(data{1,1}, data{1,2});
immse(data{1,1}, data{2,1});
immse(data{1,1}, data{2,2});
immse(data{1,2}, data{2,1});
immse(data{1,2}, data{2,2});
immse(data{2,1}, data{2,2});
I will work with larger cell arrays than in this example (e.g., 10 x 10 instead of 2 x 2). In those cases, I do not want to type out the combinations manually.
I am therefore trying to find a way / a function to automatize this process. Can anyone help?
Thanks a lot in advance.


Sign in to comment.

Accepted Answer

KSSV on 8 Jan 2021
A 2*2 matrix means there are four elements i.e. four global indices.
idx = 1:4 ;
c = nchoosek(idx,2)
C has the possible combination's indices. You can use those indices to get the required error.

More Answers (1)

Jan on 8 Jan 2021
Edited: Jan on 8 Jan 2021
This can be achieved by 2 loops using linear indexing:
for i1 = 1:numel(data)
for i2 = 1:numel(data)
immse(data{i1}, data{i2})
ind2sub let you get the 2 indices from the single linear index on demand. But 4 loops are possible also:
[s1, s2] = size(data);
for i1 = 1:s1
for i2 = 1:s2
for i3 = 1:s1
for i4 = 1:s2
immse(data{i1, i2}, data{i3, i4})

Community Treasure Hunt

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

Start Hunting!

Translated by