Create custom matrix with user-defined parameters
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I would like help creating a custom matrix that follows this pattern:
[1 1 -1 1;
1 1 1 -1;
1 2 -1 1;
1 2 1 -1;
1 3 -1 1;
1 3 1 -1;
2 1 -1 1;
2 1 1 -1;
2 2 -1 1;
2 2 1 -1;
2 3 -1 1;
2 3 1 -1;
3 1 -1 1;
3 1 1 -1;
3 2 -1 1;
3 2 1 -1;
3 3 -1 1;
3 3 1 -1;
4 1 -1 1;
4 1 1 -1;
4 2 -1 1;
4 2 1 -1;
4 3 -1 1;
4 3 1 -1]
I want the maximum number to which the first two columns count to be user-determined (e.g., x and y, respectively).
In the above example, x = 4 and y = 3.
Column one counts to x, repeating each integer (2y) times.
Column two counts to y, repeating each integer twice.
Columns three and four alternate between -1 and 1 for the entirety of the matrix.
So the total number of rows for the matrix is always (x * y * 2) which equals 24 in the above example.
Thanks in advance!
采纳的回答
madhan ravi
2020-7-3
x = 4 ;
y = 3 ;
[ X, Y ] = meshgrid( 1 : x, 1 : y ) ;
Alternator = repmat( ( - 1 ) .^ ( 1 : numel( X ) ).', 1, 2) ;
z = [ X( : ), Y( : ) ] ;
Wanted = [ repelem( z, 2, 1 ), Alternator( : ) * [ 1, - 1 ] ]
4 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!