Sorting/Arranging Data Sections in one 48772x15 matrix
이전 댓글 표시
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.
채택된 답변
추가 답변 (1개)
Sean de Wolski
2015년 4월 22일
편집: Sean de Wolski
2015년 4월 22일
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.
카테고리
도움말 센터 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!