Matrix to Vector every two rows

조회 수: 2 (최근 30일)
Mark
Mark 2013년 3월 27일
I have a matrix as follows: test2 =
0 0
0 0.0137
0 0
0 0.0081
0 0
0.0137 0.0081
I'd like to be able to create a single column vector from it of the form:
[0;0;0;0.0137;0;0;0;0.0081;0;0.0137;0;0.0081]
This is a structure taking each of the 2 rows of the matrix test2 and making them in column form, followed by the next two rows etc. So for the first 2x2 part of the matrix it would be row 1, column 1, followed by row 2, column 1, then row 1, column 2, and finally row 2, column 2 and so on through the matrix test2 until the end of the n x 2 matrix.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 27일
a=reshape(test2',2,2,[])
out=cell2mat(arrayfun(@(x) reshape(a(:,:,x)',[],1),1:size(a,3),'un',0))
out=out(:)
  댓글 수: 2
Mark
Mark 2013년 3월 27일
what is the 'un' in the arrayfun call for?
Wayne King
Wayne King 2013년 3월 27일
'uniformoutput'

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

추가 답변 (1개)

Wayne King
Wayne King 2013년 3월 27일
편집: Wayne King 2013년 3월 27일
B = reshape(test2',12,1);
I think you may have made a mistake in your example since the last two elements are 0 and 0.0081 and they should be 0.0137 and 0.0081.
Although I'm confused a bit by your post. In your text you say: "taking each of the 2 rows of the matrix test2 and making them in column form, followed by the next two rows etc."
but then later you talk about repeating row 1:
" So for the first 2x2 part of the matrix it would be row 1, column 1, followed by row 2, column 1, then row 1, column 2, "
  댓글 수: 2
Mark
Mark 2013년 3월 27일
편집: Mark 2013년 3월 27일
I want to combine it in a slightly different way actually. The two rows of column 1 followed by the two rows of column 2, then the next two rows of column 1 followed by the next two rows of column 2 and so on. so the last 4 in the matrix would be ...;0;0.0137;0;0.0081]
Mark
Mark 2013년 3월 27일
so if A = [a,b ; c,d ; e,f ; g,h ; i,j ; k,l]
I'd want B = [a ; c ; b ; d ; e ; g ; f ; h ; i ; k ; j ; l]

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

카테고리

Help CenterFile 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