Assign matrix rows/columns to separate variables - MATLAB Cody - MATLAB Central

Problem 2669. Assign matrix rows/columns to separate variables

Difficulty:Rate

This is a slight variant of this problem. Given an input array, return each row/column of the array as a separate variable. The second input variable determines whether to split on columns (dim=1) or rows (dim=2). If the number of output arguments (k) is less than the corresponding array size, only return the first k rows/columns.

Example

If

A = [1  2  3  4
     5  6  7  8
     9 10 11 12];

then

[B,C,D] = matsplit(A,1)
[E,F] = matsplit(A,2)

returns

B = [1 5 9]';
C = [2 6 10]';
D = [3 7 11]';
E = [1 2 3 4];
F = [5 6 7 8];

Solution Stats

47.32% Correct | 52.68% Incorrect
Last Solution submitted on Sep 17, 2025

Problem Comments

Solution Comments

Show comments
Join Cody Contest 2025 — Have Fun and Win Prizes!
...
We’re excited to invite you to Cody Contest 2025! 🎉 Pick a team,...
Dive Into Hands-On Learning at MATLAB EXPO 2025 – Register Now!
Get ready to roll up your sleeves at MATLAB EXPO 2025 –...
2

Problem Recent Solvers38

Suggested Problems

More from this Author44

Community Treasure Hunt

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

Start Hunting!