Divide matrix in subgroups based on rows and columns
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello everyone,
I have a 51200x48 matrix that I would like to divide by rows. More specifically, every 256 rows should be saved into a new variable (so in total 200 variables). Furthermore, I would like to do this column per column.
Can anyone help me with this? Many thanks!
2 Commenti
Steven Lord
il 25 Gen 2024
How are you hoping to use these "new variables"? If you're planning to process each segment one at a time, in a for loop for example, you may not need to create lots of new variables.
x = reshape(randperm(24), 6, 4) % Shuffle the numbers from 1 to 24 to form a 6-by-4 matrix
Here I'm not creating lots of small variables, I'm creating the one variable named data, processing the values in data, and reusing the name at the next iteration.
for r = 1:2:6
data = x([r r+1], :)
% now do stuff with data
m = max(data, [], 'all')
end
Risposte (4)
Matt J
il 25 Gen 2024
Modificato: Matt J
il 25 Gen 2024
every 256 rows should be saved into a new variable (so in total 200 variables)
I assume you mean you want them split up into cells of a cell array, not distinctly named variables (because that would be bad).
Assuming this, then you could use mat2tiles from this FEX download,
A=rand(51200,48);
Acell=mat2tiles(A,[256,1])
2 Commenti
Matt J
il 26 Gen 2024
I can't explain the error message, but you did mistype what I wrote. It should be,
Acell=mat2tiles(v_all,[256,1])
Anjaneyulu Bairi
il 25 Gen 2024
Modificato: Anjaneyulu Bairi
il 25 Gen 2024
Hi,
You can refer to the below code to create a new matrix as per the requirements mentioned below
- Every 256 rows should be saved into a new variable
- This operation should be performed column by column.
dataMatrix = rand(51200, 48); % Creates a random matrix of size (51200,48)
columnChunks = cell(48, 1); % Initialize the cell array
% Loop through each column
for col = 1:48
% Initialize a cell array for the current column
columnChunks{col} = cell(200, 1);
% Loop through each chunk for the current column and assign it to the cell array
for i = 1:200
columnChunks{col}{i} = dataMatrix((i-1)*256 + 1:i*256, col);
end
end
Hope it helps to resolve your query
Hassaan
il 25 Gen 2024
% Assuming your original matrix is named 'originalMatrix'
originalMatrix = rand(51200, 48); % Example matrix. Replace this with your actual matrix.
numRows = 256; % Number of rows in each smaller matrix
totalRows = size(originalMatrix, 1);
totalColumns = size(originalMatrix, 2);
numMatrices = totalRows / numRows; % Total number of smaller matrices
% Pre-allocate a cell array to store the smaller matrices
smallerMatrices = cell(numMatrices, totalColumns);
for col = 1:totalColumns
for i = 1:numMatrices
startRow = (i - 1) * numRows + 1;
endRow = i * numRows;
smallerMatrices{i, col} = originalMatrix(startRow:endRow, col);
end
end
disp(smallerMatrices(1:5))
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
2 Commenti
Hassaan
il 26 Gen 2024
Modificato: Hassaan
il 26 Gen 2024
% Assuming your original matrix is named 'originalMatrix'
originalMatrix = rand(51200, 48); % Example matrix. Replace this with your actual matrix.
numRows = 256; % Number of rows in each smaller matrix
totalRows = size(originalMatrix, 1);
totalColumns = size(originalMatrix, 2);
numMatrices = totalRows / numRows; % Total number of smaller matrices
% Pre-allocate a cell array to store the smaller matrices
smallerMatrices = cell(numMatrices, totalColumns);
for col = 1:totalColumns
for i = 1:numMatrices
startRow = (i - 1) * numRows + 1;
endRow = i * numRows;
smallerMatrices{i, col} = originalMatrix(startRow:endRow, col);
end
end
disp(smallerMatrices(1:5))
% Assuming you have the 'smallerMatrices' from your previous code
% Pre-allocate an array to store the standard deviations
stdDeviations = zeros(numRows, 1);
for elementIdx = 1:numRows
elements = zeros(numMatrices, 1);
for matrixIdx = 1:numMatrices
% Extract the 'elementIdx'-th element from each matrix
elements(matrixIdx) = smallerMatrices{matrixIdx}(elementIdx);
end
% Calculate the standard deviation for this element across all matrices
stdDeviations(elementIdx) = std(elements);
end
disp(stdDeviations)
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
Voss
il 25 Gen 2024
A = rand(51200,48);
n_rows_per_cell = 256;
n_cols_per_cell = 1;
[n_rows,n_cols] = size(A);
Acell = mat2cell(A, ...
n_rows_per_cell*ones(1,n_rows/n_rows_per_cell), ...
n_cols_per_cell*ones(1,n_cols/n_cols_per_cell))
0 Commenti
Vedere anche
Categorie
Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!