Question
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.
Expert Answer
Prashant Kumar
PhD Expert
Answered Aug 29, 2026
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 ] ]
100% Run Guarantee
3-Hour Fast-Track Delivery
Need a Custom Version or Complete Simulation for This Problem?
Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.
Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here
Related matrix Questions & Solutions
Browse All →
Explore similar technical troubleshooting questions and verified MATLAB solutions:
Ready-to-Run MATLAB & Simulink Projects
Browse All Projects →