convert a column matrix with many rows into multiple column of equal rows

14 views (last 30 days)
Suppose i have a column matrix A with 10 row and 1 column
A = [1;3;2;4;5;8;7;6;12;10]
Now i want to split the above column matrix into two columns with first 5 numbers in one column and second 5 numbers in 2nd column stored in B such that
B = [1 8;3 7;2 6;4 12;5 10]
Also Now i need a generalized code so that i can change it for example if i have a column matrix C with (1500*1 double) i need this splitted like first 15values in 1st column and second 15 values in 2nd column and so on.. i will get 100 columns with 15 rows each stored in variable D. Hope this explanation is good. Thank you in advance.

Accepted Answer

Stephen23
Stephen23 on 20 Jun 2023
A = [1;3;2;4;5;8;7;6;12;10]
A = 10×1
1 3 2 4 5 8 7 6 12 10
B = reshape(A,[],2)
B = 5×2
1 8 3 7 2 6 4 12 5 10
B = [1,8;3,7;2,6;4,12;5,10]
B = 5×2
1 8 3 7 2 6 4 12 5 10

More Answers (2)

Alan Stevens
Alan Stevens on 20 Jun 2023
Try the reshape function.

Shishir Reddy
Shishir Reddy on 20 Jun 2023
Edited: Shishir Reddy on 20 Jun 2023
Hi barath,
To split a column matrix into multiple columns, each containing a specific number of rows, you can use the reshape function in MATLAB. Here's a code snippet that demonstrates the process:
% Example with matrix A
A = [1;3;2;4;5;8;7;6;12;10];
% Specify the number of rows for each column
numRows = 5;
% Determine the number of columns required based on the specified number of rows
numColumns = ceil(numel(A) / numRows);
% Pad the matrix A with zeros if the number of elements is not divisible by numRows
A_padded = [A; zeros(numRows*numColumns - numel(A), 1)];
% Reshape the padded matrix into the desired format
B = reshape(A_padded, numRows, numColumns)';
% Display the result
disp(B);
This code first determines the number of columns required ('numColumns') based on the specified number of rows ('numRows'). It then pads the original matrix 'A' with zeros to make the number of elements divisible by 'numRows'. Next, the 'reshape' function is used to split the padded matrix into the desired format. Finally, the resulting matrix 'B' is displayed.
To generalize this code for any column matrix C with N rows, you can modify the code as follows:
% Example with matrix C
C = randi(100, 1500, 1); % Random column matrix with 1500 rows
% Specify the number of rows for each column
numRows = 15;
% Determine the number of columns required based on the specified number of rows
numColumns = ceil(numel(C) / numRows);
% Pad the matrix C with zeros if the number of elements is not divisible by numRows
C_padded = [C; zeros(numRows*numColumns - numel(C), 1)];
% Reshape the padded matrix into the desired format
D = reshape(C_padded, numRows, numColumns)';
% Display the result
disp(D);
This code follows the same logic as the previous example, but it works with a generalized column matrix 'C' with 'N' rows. It generates a random column matrix with 1500 rows for demonstration purposes. You can replace 'C' with your actual column matrix. The resulting matrix 'D' will have 100 columns, each containing 15 rows.
For further reference, you can go through the 'reshape' function https://in.mathworks.com/help/matlab/ref/reshape.html
I hope this resolves the issue.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!