필터 지우기
필터 지우기

Removing columns from a matrix

조회 수: 1 (최근 30일)
Atreyu91
Atreyu91 2015년 8월 22일
댓글: Star Strider 2015년 8월 23일
Can anyone please how me how to remove columns from a matrix?
an example of what I am trying to do is remove the 4 columns of this 2x6 matrix:
[a,0,0,0,0,b; c,0,0,0,0,d]
to
[a,b;c,d]

채택된 답변

Star Strider
Star Strider 2015년 8월 22일
One possibility:
[a,b,c,d] = deal(3,5,7,13); % Populate Variables
M = [a,0,0,0,0,b; c,0,0,0,0,d];
Mresult = reshape(M(M~=0), size(M,1), [])
Mresult =
3 5
7 13
  댓글 수: 3
Atreyu91
Atreyu91 2015년 8월 23일
Do you know how to reverse this process and add the zeros back into the matrix?
Star Strider
Star Strider 2015년 8월 23일
My pleasure!
You have to know the number of zeros you want to add. That information was lost in the first step. Probably the easiest way to do it though would be something like this:
Mreverse = zeros(2,6);
Ix = sub2ind(size(Mreverse), [1 1 2 2], [1 6 1 6]);
Mreverse(Ix) = [a b c d];
There are probably a number of ways to do it, this is one.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by