Sorting/Arranging Data Sections in one 48772x15 matrix
Mostrar comentarios más antiguos
Hello,
I have a 48772x15 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.
Respuesta aceptada
Más respuestas (1)
Sean de Wolski
el 22 de Abr. de 2015
Editada: Sean de Wolski
el 22 de Abr. de 2015
You could absolutely do this with a for-loop and if else. Here's a vectorized approach that bins based on index directly:
x = [magic(6) [1; 0; 0; 2; 0; 1]]; % simple easy to understand example
idx = logical(x(:,end)); % Where are the non zero values
bin = x(idx,end); % which bin?
idxx = cumsum(idx); % group zeros with previous
C = accumarray(bin(idxx),(1:numel(idxx)),[],@(v){x(sort(v),:)}) % aggregate
C{:}
C is a cell array, the first element C{1} corresponds to the 1s, etc. This approach requires that the first element in the last column be non-zero. You could remove the earlier rows if this is not the case.
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!