matrix array
조회 수: 2 (최근 30일)
이전 댓글 표시
Dear Sir I need some help for matrix array. I have matrix 23x2 like this: 12 0 8 0 4 0 1 0 5 0 9 0 3 0 1 0 10 0 12 0 2 5 4 7 6 9 8 11 2 3 6 7 10 11 1 2 3 4 5 6 7 8 9 10 11 12
And I would like to separate it to two matrixes
1) With zeros in second Colum:
A=
12 0
8 0
4 0
1 0
5 0
9 0
3 0
1 0
10 0
12 0
2) Without zeros in second Colum:
B=
2 5
4 7
6 9
8 11
2 3
6 7
10 11
1 2
3 4
5 6
7 8
9 10
11 12
I have been used, nonzero but it will change my dimension to only one column. I need only non zeros in second Colum and value in first Colum that correspond to non-zeros in second Colum.
Anyone can suggest me please.
Thank you very much
댓글 수: 0
답변 (2개)
Jarrod Rivituso
2011년 5월 18일
Would reshape followed by transpose do the trick....
%Create data array
data = [12 0 8 0 4 0 1 0 5 0 9 0 3 0 1 0 10 0 12 0 2 5 4 7 6 9 8 11 2 3 6 7 10 11 1 2 3 4 5 6 7 8 9 10 11 12]
%Reshape, then transpose
A = reshape(data(1:20),2,10)'
B = reshape(data(21:46),2,13)'
댓글 수: 0
Andrei Bobrov
2011년 5월 18일
variant
A = [12 0 8 0 4 0 1 0 5 0 9 0 3 0 1 0 10 0 12 0 ...
2 5 4 7 6 9 8 11 2 3 6 7 10 11 1 2 3 4 5 6 7 8 9 10 11 12];
N =find(A==0,1,'last');
A1 = reshape(A(1:N),2,[])';
A2 = reshape(A(N+1:end),2,[])';
If the matrix Ainput is as follows:
Ainput = [12 0
8 0
4 0
1 0
5 0
9 0
3 0
1 0
10 0
12 0
2 5
4 7
6 9
8 11
2 3
6 7
10 11
1 2
3 4
5 6
7 8
9 10
11 12];
then
N = find(all(Ainput,2),1,'first')-1
A1 = Ainput(1:N,:)
A2 = Ainput(N+1:end,:)
if
Ainput = [12 7
0 6
8 9
0 8
4 11
0 2
1 3
0 6
5 7
0 10
9 11
0 1
3 2
0 3
1 4
0 5
10 6
0 7
12 8
0 9
2 10
5 11
4 12]
as at the beginning of this an answer
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!