How to reshape rows into columns

How can I change the following matrix:
[1, 5,0,0; 2,4,0,0; 3, 2,6,4]
to look like:
[1,5; 2, 4; 3,2; 3,6; 3,4]?
Any suggestion would be great. Thanks

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 9월 1일

0 개 추천

A =[1, 5,0,0; 2,4,0,0; 3, 2,6,4]
[i1 j1] = find(A(:,2:end))
sz = size(A)
out = sortrows([A(i1,1) A(sz(1)+sub2ind(sz- [0 1],i1,j1))],1)
more
[i2 j2 c] = find(A)
t = j2~=1
out = sortrows([A(i2(t),1) c(t)],1)
variant 3
AA = A(:,2:end)
[i1,~] = find(AA)
out = sortrows([A(i1,1) reshape(AA(AA~=0),[],1)],1)

댓글 수: 3

Amandeep
Amandeep 2011년 9월 1일
Thanks for your quick response.
But waht if instead:
A = [1,5,0,0;3,4,0,0;5,2,6,4]
Andrei Bobrov
Andrei Bobrov 2011년 9월 1일
>> A = [1,5,0,0;3,4,0,0;5,2,6,4];
[i2 j2 c] = find(A);
t = j2~=1;
out = [A(i2(t),1) c(t)]
out =
1 5
3 4
5 2
5 6
5 4
Amandeep
Amandeep 2011년 9월 1일
I think I nearly figured it out
A = [1,5,0,0;3,4,0,0;5,2,6,4]
AA = permute(A(:,2:end),[2,1])
AA = reshape(AA,numel(A(:,2:end)),1)
zero = (AA==0)
AA(zero) = []
Now just trying to create the first column

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

추가 답변 (1개)

Sivakumaran Chandrasekaran
Sivakumaran Chandrasekaran 2011년 9월 1일

0 개 추천

Hi, There is a command named reshape. Check with it.
Regards, Sivakumaran vlsiva@yahoo.co.in

댓글 수: 2

Amandeep
Amandeep 2011년 9월 1일
How do I use reshape on the third row?
Oleg Komarov
Oleg Komarov 2011년 9월 1일
@Siva: if I were you I wouldn't leave my mail on a forum.

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by