フィルターのクリア

Create a permutation of elements in the same position

2 ビュー (過去 30 日間)
Salwa Mostafa
Salwa Mostafa 2022 年 2 月 15 日
コメント済み: Salwa Mostafa 2022 年 2 月 15 日
I have coordinates of points x = [x_1 x_2], y =[ y_1 y_2 ] z = [z_1 z_2 ]
I want to create a matrix with all permutation but the coordinates are in the same position
[ x_1 y_1 z_2;
x_1 y_2 z_2;
x_1 y_1 z_1;
x_1 y_2 z_2;
x_2 y_1 z_2;
x_2 y_2 z_2;
x_2 y_1 z_1;
x_2 y_2 z_2; ]
but the coordinates is changing so it can be x,y,z,v,w I want to write it for a general number of coordinates.

採用された回答

William Rose
William Rose 2022 年 2 月 15 日
I see that you have already done it for 2 pairs of coordinates. Here is a way to do it for N coordinates.
N=3;
x=rand(1,N); y=rand(1,N); z=rand(1,N);
A=zeros(N^3,3); %initialize array
for i=1:N
for j=1:N
for k=1:N
A(N^2*(i-1)+N*(j-1)+k,:)=[x(i),y(j),z(k)];
end
end
end
Try it. Good luck.

その他の回答 (2 件)

Yongjian Feng
Yongjian Feng 2022 年 2 月 15 日
Just 3 for loops.

DGM
DGM 2022 年 2 月 15 日
編集済み: DGM 2022 年 2 月 15 日
Consider the example:
% input coordinates
x = [1 2];
y = [11 22];
z = [111 222];
% create the index array
m = (dec2bin(0:7,3) == '1') + 1;
% all combinations in order
xyz = [x(m(:,1)); y(m(:,2)); z(m(:,3))].'
xyz = 8×3
1 11 111 1 11 222 1 22 111 1 22 222 2 11 111 2 11 222 2 22 111 2 22 222
Or more generally:
x = [1 2 3];
y = [11 22 33];
z = [111 222 333];
% create the index array
nx = numel(x);
m = repmat((1:nx)',[nx*2 1]);
m = [repelem(m(1:size(m,1)/nx,1),nx) m];
m = [repelem(m(1:size(m,1)/nx,1),nx) m];
% get the combinations like before
xyz = [x(m(:,1)); y(m(:,2)); z(m(:,3))].'
xyz = 18×3
1 11 111 1 11 222 1 11 333 1 22 111 1 22 222 1 22 333 1 33 111 1 33 222 1 33 333 2 11 111
  3 件のコメント
DGM
DGM 2022 年 2 月 15 日
I added an example for that as well.
Salwa Mostafa
Salwa Mostafa 2022 年 2 月 15 日
Thanks so much

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

カテゴリ

Help Center および File ExchangeCartesian Coordinate System Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by