Get the index of paired Values using allcomb

4 ビュー (過去 30 日間)
N/A
N/A 2016 年 12 月 20 日
コメント済み: N/A 2016 年 12 月 20 日
Greetings!
I am using the allcomb function (see here: http://de.mathworks.com/matlabcentral/fileexchange/10064-allcomb-varargin-) to pair values between an arbitrary number of vectors. Works perfectly, I get a resulting array with all possible combinations, but in addition to that I do need the information as to which values actually got paired, so basically the index from the original vectors for every entry in the resulting array. The background is that a lot of values in the original vectors are the same but it still matters for my application which position it is at.
Anybody has an idea on how to do that? Doesn't matter if the informations are added to the same array or in a separate one.
Any help would be much appreciated, thank you!

採用された回答

Guillaume
Guillaume 2016 年 12 月 20 日
Instead of creating the cartesian product of the vectors, create the cartesian product of the indices of the vector:
vectors = {randi(100, 1, 20), randi(100, 1, 10), randi(100, 1, 7)}; %demo data
indices = cellfun(@(v) 1:numel(v), vectors, 'UniformOutput', false); %vector of indices
combindices = allcomb(indices{:}); %cartesian product of indices
Then either use this cartesian product to index your vector, or call allcomb on your vector, whichever is faster
%indexing with combindices:
combvectors = cell2mat(cellfun(@(v, i) reshape(v(i), [], 1), vectors, num2cell(combindices, 1), 'UniformOutput', false));
%or call allcomb again
combvectors = allcomb(vectors{:});
  1 件のコメント
N/A
N/A 2016 年 12 月 20 日
Exactly what I have been looking for, perfect, thanks so much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by