matrix array

4 views (last 30 days)
Worravit nova
Worravit nova on 18 May 2011
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

Answers (2)

Jarrod Rivituso
Jarrod Rivituso on 18 May 2011
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)'

Andrei Bobrov
Andrei Bobrov on 18 May 2011
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

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!