MATLAB: Sorting/Arranging Data Sections in one 48772×15 matrix

elseelseifgrab dataiflarge matrixsortsort data

Hello,
I have a 48772×15 matrix with data in columns 1-14. In column 15 there are 0s with the occasional nonzero number (from 1-8) as a marker for a project I'm doing. What I need help with is when there is a nonzero number I want to take/grab the data from all columns from that row until there is another nonzero number in that column and put that data into a different area so I can sort it. ie. when marker number 6 shows up I want to take that data (including the row with the marker) until there is another nonzero marker in that column (excluding the data starting with the row with the next marker) and then put that data with the other number 6's data.
I am not that proficient in matlab so any help would be beneficial. I know I should use an else if statement and grab data but not sure how to formulate it.
Thank you.

Best Answer

This seems to work, and runs quickly:
D = load('Analysis_of_Excel.mat');
M = D.Analysis_of_Excel;
M15 = [find(M(:,15) > 0); size(M,1)+1]; % Need Row Indices
for k1 = 1:8
Ix1 = find(M(:,15) == k1); % Row Blocks Starting With ‘k1’
for k2 = 1:length(Ix1)
Ix2 = M15(find(M15 > Ix1(k2), 1, 'first'));
Data{k1,k2} = M(Ix1:Ix2-1,:);
end
end
I can’t think of a way to do it without the nested for loops, because it is necessary to determine the first row index equal to ‘k1’, then find all locations of that number (in ‘Ix1’) and look for the next non-zero value in Column 15, the function of the ‘k2’ loop that determines ‘Ix2’. The rest is straightforward. The ‘M15’ vector stores the indices of all the non-zero values in ‘M(:,15)’.
The ‘Data’ matrix has the values of the ‘M(:,15)’ indicators as the row index (so ‘1’ is row #1, etc.) with the data for each value across the columns. This looks correct, but it’s difficult to check manually because the matrix is so large. If there are problems, post them and I will do the best I can to resolve the problems with my code.