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.
댓글 수: 0
답변 (1개)
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);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!