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

2 次查看(过去 30 天)
Hello,
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.

采纳的回答

KSSV
KSSV 2021-1-8
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.

更多回答(1 个)

Jan
Jan 2021-1-8
编辑:Jan 2021-1-8
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})
end
end
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})
end
end
end
end

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by