convert a column matrix with many rows into multiple column of equal rows
15 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
barath manoharan
el 20 de Jun. de 2023
Comentada: barath manoharan
el 22 de Jun. de 2023
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 comentarios
Respuesta aceptada
Stephen23
el 20 de Jun. de 2023
A = [1;3;2;4;5;8;7;6;12;10]
B = reshape(A,[],2)
B = [1,8;3,7;2,6;4,12;5,10]
Más respuestas (2)
Shishir Reddy
el 20 de Jun. de 2023
Editada: Shishir Reddy
el 20 de Jun. de 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.
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!