필터 지우기
필터 지우기

3D to 2D matrix and then drop the NANS?

조회 수: 2 (최근 30일)
Islam
Islam 2013년 11월 29일
답변: Wayne King 2013년 11월 29일
I have b(j,k,w) 3D matrix (3,4,2), I want to convert it to one line vector and then drop the NAN elements
The 3D matrix is
b(:,:,1) =
218. 129. 142. 63.
NaN NaN NaN NaN
NaN NaN NaN NaN
b(:,:,2) =
140 109 119 61
NaN NaN NaN NaN
NaN NaN NaN NaN
I want to reshape the b(:,:,1) in one line then b(:,:,2) , combine them in one line and then drop the NANS
I tried to :- convert it to 2D matrix with the full set reshape(b,[w ,k*j])
ans =
218. 129. 142. 63. 140 109 119 61
NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN
Now I want it to be like this, but I couldn't keep the order of the lines as shown
218. 129. 142. 63. NaN NaN NaN NaN NaN NaN NaN NaN 140 109 119 61 71 NaN NaN NaN NaN NaN NaN NaN NaN
and then drop the NANS to end with this (how to drop them ?)
218. 129. 142. 63. 140 109 119 61 71

채택된 답변

Wayne King
Wayne King 2013년 11월 29일
You can just reshape the transposes of each "page"
c1 = reshape(b(:,:,1)',12,1);
c2 = reshape(b(:,:,2)',12,1);
C = [c1; c2];
C = C(~isnan(C));

추가 답변 (1개)

Wayne King
Wayne King 2013년 11월 29일
A = reshape(b,24,1);
A = A(~isnan(A));
  댓글 수: 1
Islam
Islam 2013년 11월 29일
편집: Islam 2013년 11월 29일
your solutions works for the above example, but assume the 3D matrix is this instead:
b(:,:,1) =
218. 129. 142. 63.
NaN NaN NaN NaN
30 500 120 10
b(:,:,2) =
140 109 119 61
120 500 10 20
NaN NaN NaN NaN
when converting to 2D with the full set using reshape reshape(b,[w ,k*j])
it will become : [
218 129 142 63 140 109 119 61
NaN NaN NaN NaN 120 500 10 20
30 500 120 10 NaN NaN NaN NaN ]
The problem here is I want to keep it one line in this order first [ 218 129 142 63 NaN NaN NaN NaN 30 500 120 10 140 109 119 61 120 500 10 20 NaN NaN NaN NaN ]
and then drop the NAN

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

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by