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