How to create a matrix using vectors(columns) from different matrices (in a structure) with different lengths?
1 view (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
BhaTTa
on 23 Jul 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);
0 Comments
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!