convert a column matrix with many rows into multiple column of equal rows
15 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
更多回答(2 个)
Shishir Reddy
2023-6-20
编辑:Shishir Reddy
2023-6-20
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.
另请参阅
类别
在 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!