How to create a matrix using vectors(columns) from different matrices (in a structure) with different lengths?

1 vue (au cours des 30 derniers jours)
So I have a structure that contain in its different cell a matrix os 2 columns and AROUND 1000 rows each. I want to be able to get the second column of each cell to create a matrix. I tried this using a look with an indexed matrix but it replies an error indicating that the size does not match.

Réponses (1)

BhaTTa
BhaTTa le 23 Juil 2024
Here is the code to achieve it:
S = {rand(1000, 2), rand(1000, 2), rand(1000, 2)};
% Number of cells in the cell array
num_cells = numel(S);
% Initialize an empty array to store the second columns
% Assuming all matrices have the same number of rows
num_rows = size(S{1}, 1);
second_columns = zeros(num_rows, num_cells);
% Loop through each cell and extract the second column
for i = 1:num_cells
second_columns(:, i) = S{i}(:, 2);
end
% Display the resulting matrix
disp('Matrix of second columns:');
disp(second_columns);

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by