How do you use multiple indices to separate data and put in a structure?
조회 수: 3 (최근 30일)
이전 댓글 표시
I have a 3672 x10 matrix (A). I have a 1x300 array of indices (B). What I would like to do is get the data in matrix A between the indices in B and put each into a strucure. For example, I would like the first cell in the structure to contain these data:
A(B(1,1):B(1,2)-1,:)
I would like the second cell in the structure to contain these data:
A(B(1,2):B(1,3)-1,:)
I would like the third cell in the structure to contain these data:
A(B(1,3):B(1,4)-1,:)
...and so on thus that the last cell in the structure is
A(B(1,300):end,:)
So the structure will have 300 cells where each cell contains different snippets of data as defined above.
댓글 수: 0
채택된 답변
the cyclist
2020년 5월 11일
편집: the cyclist
2020년 5월 11일
Here's one straightforward way
% Make up some data and indices
A = rand(3672,10);
B = 1:2:600;
% Preallocate the cell array
C = cell(300,1);
% Assign the cell contents
for ii = 1:299
C{ii} = A(B(1,ii):B(ii+1)-1,:);
end
% Assign the last cell, which is a special case
C{300} = A(B(1,300):end,:);
댓글 수: 0
추가 답변 (1개)
the cyclist
2020년 5월 11일
I was pretty sure there was a simple vectorized way to do this, but it did not come to me right away. But then I remembered it:
% Make up some data and indices
A = rand(3672,10);
B = 1:2:600;
rowCountByCell = diff([B size(A,1)+1]);
C = mat2cell(A,rowCountByCell);
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!