Cell array of coordinates from two vectors

9 ビュー (過去 30 日間)
LC
LC 2018 年 10 月 5 日
I have two vectors, one with 119 X values and a one with 102 Y values. I'm trying to work out a way to combine these to create to all possible unique sets of coordinates (12,138 unique coordinates). I could use two nested for loops to do it, but it's very slow. I'm struggling to find if theres any combination of matlab functions that could achieve this.
  1 件のコメント
KSSV
KSSV 2018 年 10 月 5 日
Have a look on perms

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

採用された回答

Stephen23
Stephen23 2018 年 10 月 5 日
編集済み: Stephen23 2018 年 10 月 5 日
No loops required, just use ndgrid and indexing:
>> X = randi(999,119,1); % must be column vector
>> Y = randi(999,102,1); % must be column vector
>> [idx,idy] = ndgrid(1:numel(X),1:numel(Y));
>> M = [X(idx(:)),Y(idy(:))];
>> size(M)
ans =
12138 2
Here is a simpler example:
>> X = (1:5).';
>> Y = (6:9).';
>> [idx,idy] = ndgrid(1:numel(X),1:numel(Y));
>> M = [X(idx(:)),Y(idy(:))];
>> M
M =
1 6
2 6
3 6
4 6
5 6
1 7
2 7
3 7
4 7
5 7
1 8
2 8
3 8
4 8
5 8
1 9
2 9
3 9
4 9
5 9
  2 件のコメント
LC
LC 2018 年 10 月 5 日
Thanks a lot!
shashikiran mudavath
shashikiran mudavath 2020 年 9 月 21 日
How to acess M ?

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

その他の回答 (1 件)

Raghunandan V
Raghunandan V 2018 年 10 月 5 日
you should you at least one nested loop
for k=1:length(X)
z=[X(k) Y'];
perms(Z);
end

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by