How can I repeat row and column extraction of a matrix until the end of the matrix is reached?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
wbenn7
el 18 de Sept. de 2024
Comentada: Voss
el 18 de Sept. de 2024
I have a 1015x2 matrix of data and I'd like to extract the data in 145x2 incraments and add them to another matrix until I've reached the end of the original 1015x2 matrix. The next 145x2 extraction would then be added to 3rd and 4th columns of the new matrix. The resulting matrix should end up being of size 145x14. Thank you!
0 comentarios
Respuesta aceptada
Vandit
el 18 de Sept. de 2024
You can achieve this by using a combination of indexing and concatenation in MATLAB. Here's an example of how you can extract and add the data in increments of 145x2 to create a new matrix of size 145x14:
% Original matrix of size 1015x2
originalMatrix = rand(1015, 2);
% Initialize the new matrix
newMatrix = [];
% Extract and add data in increments of 145x2
for i = 1:145:size(originalMatrix, 1)
data = originalMatrix(i:i+144, :);
% Add the data to the new matrix
newMatrix = [newMatrix, data];
end
In the above example, 'originalMatrix' is a matrix with dimensions 1015x2. An empty 'newMatrix' is initialized, and a for loop is used to extract and append data in increments of 145x2. The extracted data is stored in the data variable and concatenated with 'newMatrix' using the concatenation operator [].
After the loop completes, 'newMatrix' will have dimensions 145x14, with each 145x2 increment added as columns to form the new matrix.
For more information on 'Creating, Concatenating, and Expanding Matrices', please refer to the below documentation:
Hope this helps.
Más respuestas (1)
Voss
el 18 de Sept. de 2024
M = rand(1015,2);
incr = 145;
m = size(M,1)/incr;
n = size(M,2);
newM = reshape(permute(reshape(M,[],m,n),[1 3 2]),[],m*n);
2 comentarios
Voss
el 18 de Sept. de 2024
Thanks!
This way's probably more natural:
M = rand(1015,12);
incr = 145;
newM = reshape(permute(reshape(M,incr,[],size(M,2)),[1 3 2]),incr,[]);
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!