2D matrix to 3D matrix given index in the 2D matrix col1

조회 수: 1 (최근 30일)
Dave
Dave 2014년 12월 17일
댓글: Dave 2014년 12월 18일
Hello, I need to construct a 3D matrix using a 2D matrix.
As an example, take matrix A which is 7x4.
First element of first column (date format) is repeated at most 2 times in all cases.
Am looking for the last 2 elements of each row to finally form layers of 2x2 matrices, one for each date:
A= ...
[735950 1 3 5;
735951 1 3 5;
735953 1 4 6;
735950 2 1 7;
735951 2 2 8;
735952 2 5 9;
735953 2 3 9]
So, in the first case (735950) I need to form a 3D matrix B where the FIRST layer is
B(:,:,1)= ...
[3 5;
1 7]
Note A is not in ascending order. For this FIRST layer the 1st row of B is in row1 of A, but the 2nd of B row is in row4 of A.
The second case (735951) SECOND layer is
B(:,:,2)= ...
[3 5 ;
2 8]
The third case (735952) since it is only repeated one time, should be ignored
The fourth case (735953) should be the THIRD layer
B(:,:,3)= ...
[4 6 ;
3 9]
Thanks for any suggestions.

채택된 답변

Shoaibur Rahman
Shoaibur Rahman 2014년 12월 18일
C = sortrows(A,1);
indx = 0;
for k = 1:size(C,1)-1
if C(k,1) == C(k+1,1)
indx = indx+1;
B(:,:,indx) = C(k:k+1,end-1:end);
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by