create a vector ,of sets of predefined numbers stored in another row vectors
1 回表示 (過去 30 日間)
古いコメントを表示
Guys,I have two row vectors X=[3:6] and Y=[8:27].I want to create and store all possible combinations of (X,Y) in another vector ,say Z.
So my Z will be, Z=[(3,8) (3,9) (3,10)...(3,27) (4,8) (4,9)......(6,26) (6,27)]
0 件のコメント
採用された回答
Guillaume
2015 年 3 月 3 日
x = 3:6;
y = 8:27;
[xx, yy] = ndgrid(x, y);
cartprod = [xx(:) yy(:)]
This works also for more than two vectors:
x = 3:6;
y = 8:27;
z = 2:2:8;
[xx, yy, zz] = ndgrid(x, y, z);
cartprod = [xx(:) yy(:) zz(:)]
その他の回答 (1 件)
James Tursa
2015 年 3 月 3 日
編集済み: James Tursa
2015 年 3 月 3 日
Not sure how you want the answer actually stored (the syntax in your example isn't clear), but here is one way:
XX = repmat(X(:)',numel(Y),1);
YY = repmat(Y(:),1,numel(X));
Z = [XX(:) YY(:)];
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!