I have a matrix that looks like this:
M = [1 4 56 1; 1 3 5 1; 1 3 6 4; 2 3 5 0; 2 0 0 0; 3 1 2 3; 3 3 3 3]
I want to split this matrix into the number indicated in the first column. So I want to break the matrix into this:
A = [1 4 56 1; 1 3 5 1; 1 3 6 4] B = [2 3 5 0; 2 0 0 0] C = [3 1 2 3; 3 3 3 3]
I tried this by running the following loop, but this gave me the needed matrices with rows of zeros:
for i = 1:length(M) if (M(i,1) == 1) A(i,:) = M(i,:); elseif (M(i,1) == 2) B(i,:) = M(i,:); elseif (M(i,1) == 3) C(i,:) = M(i,:); end end
The result for matrix C is, for example, the following:
C = [0 0 0 0; 0 0 0 0; 0 0 0 0; 2 3 5 0; 2 0 0 0]
How do I solve this problem?
Additional Information:
The actual data has a date in the first column in the form yyyymmdd . The data set covers several years, and I want to break this data set in matrices for each year, and then for each month.
source share