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

1 次查看(过去 30 天)
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.

回答(1 个)

BhaTTa
BhaTTa 2024-7-23
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);

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by