need general coding/ command to separate matrix data
조회 수: 2 (최근 30일)
이전 댓글 표시
I have a for loop y= 0:2:8 and whenever the for loop value chnaged it generates two values which was stored in a matrix. Now I have a complete matrix in which column one have y=0 then several values in a rows then y changed and several vlaues in a row as shown in fig.
final matrix is:
0 5 9
0 7 3.3
0 5.5 9
1 4.0 3.5
1 2 3.3
...................... so on.
Question: I need a general coding that store number of rows in a seperate matrix/ vector when y value is fixed. I mean when y=0 then all rows with y=0 stored in a seperate vector/ matrix. Then again y changed and results in several rows. These rows stored in a seperate vector/matrix.
Your support will be highly appreciated.
Regards
댓글 수: 4
Stephen23
2022년 9월 7일
Keeping data together is usually better than splitting it apart. How will you process the data?
채택된 답변
Bruno Luong
2022년 9월 7일
편집: Bruno Luong
2022년 9월 7일
Try this:
% Fake data
finalmatrix=sortrows(randi(10,30,3),1)
y = finalmatrix(:,1);
n=diff(find([true; diff(y)~=0; true]))
splitmatrix=mat2cell(finalmatrix,n,size(finalmatrix,2));
splitmatrix{:}
댓글 수: 4
dpb
2022년 9월 7일
I got totally lost in the explanation above, but if @Bruno Luong's solution for splitmatrix is the right data, then
figure
hold on
cellfun(@(c)plot(c(:,2),c(:,3)),splitmatrix)
should be close to what you're looking for.
As always, "salt to suit"...
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!