필터 지우기
필터 지우기

How to partition a matrix by sorting a column?

조회 수: 1 (최근 30일)
Wan-Yi Chiu
Wan-Yi Chiu 2024년 2월 24일
이동: Stephen23 2024년 2월 24일
Dear friends:
I want to parttion a matrix into two submatrix by sorting the third column:
For example, the matrix is as follows:
A= [ 73.90 123.17 1.00;
73.79 121.83 0.00;
70.64 74.46 1.00;
69.74 86.40 0.00]
I need the output as:
A1= [ 73.90 123.17 1.00;
70.64 74.46 1.00]
and
A2= [ 73.79 121.83 0.00;
69.74 86.40 0.00]
Thank you very much.

채택된 답변

Walter Roberson
Walter Roberson 2024년 2월 24일
A= [ 73.90 123.17 1.00;
73.79 121.83 0.00;
70.64 74.46 1.00;
69.74 86.40 0.00]
A = 4×3
73.9000 123.1700 1.0000 73.7900 121.8300 0 70.6400 74.4600 1.0000 69.7400 86.4000 0
u = unique(A(:,3));
A1 = A(A(:,3)==u(2),:)
A1 = 2×3
73.9000 123.1700 1.0000 70.6400 74.4600 1.0000
A2 = A(A(:,3)==u(1),:)
A2 = 2×3
73.7900 121.8300 0 69.7400 86.4000 0
  댓글 수: 1
Wan-Yi Chiu
Wan-Yi Chiu 2024년 2월 24일
이동: Stephen23 2024년 2월 24일
Dear friends
Thanks to you.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Voss
Voss 2024년 2월 24일
A= [ 73.90 123.17 1.00;
73.79 121.83 0.00;
70.64 74.46 1.00;
69.74 86.40 0.00]
A = 4×3
73.9000 123.1700 1.0000 73.7900 121.8300 0 70.6400 74.4600 1.0000 69.7400 86.4000 0
C = splitapply(@(x){x},A,findgroups(A(:,end)));
Cell array C contains the matrices you want, A1 and A2
C{:}
ans = 2×3
73.7900 121.8300 0 69.7400 86.4000 0
ans = 2×3
73.9000 123.1700 1.0000 70.6400 74.4600 1.0000
so you don't need to make them separate new variables, but you can
A1 = C{2}
A1 = 2×3
73.9000 123.1700 1.0000 70.6400 74.4600 1.0000
A2 = C{1}
A2 = 2×3
73.7900 121.8300 0 69.7400 86.4000 0

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by