Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
select a submatrix with values different from 0

Subject: select a submatrix with values different from 0

From: Patrick Diviacco

Date: 30 Oct, 2009 20:21:02

Message: 1 of 5

How can I create a new matrix from this matrix, with values different from 0 ?

(I assume they are adjacent to each other)

A =
0 0 0 0 0 0 0
0 2 3 4 0 0 0
0 3 1 2 0 0 0
0 2 3 1 0 0 0
0 0 0 0 0 0 0

I want

B =

2 3 4
3 1 2
2 3 1


thanks

Subject: select a submatrix with values different from 0

From: Patrick Diviacco

Date: 30 Oct, 2009 20:32:01

Message: 2 of 5

And the second question is, how can I restore the old matrix having the following matrices ?

I have


A =
0 0 0 0 0 0 0
0 1 1 1 0 0 0
0 1 1 1 0 0 0
0 1 1 1 0 0 0
0 0 0 0 0 0 0


B =

2 3 4
3 1 2
2 3 1



I want

C =
0 0 0 0 0 0 0
0 2 3 4 0 0 0
0 3 1 2 0 0 0
0 2 3 1 0 0 0
0 0 0 0 0 0 0



thanks

Subject: select a submatrix with values different from 0

From: Nathan

Date: 30 Oct, 2009 21:03:03

Message: 3 of 5

On Oct 30, 1:32 pm, "Patrick Diviacco" <patrick.divia...@gmail.com>
wrote:
> And the second question is, how can I restore the old matrix having the following matrices ?
>
> I have
>
> A =
> 0 0 0 0 0 0 0
> 0 1 1 1 0 0 0
> 0 1 1 1 0 0 0
> 0 1 1 1 0 0 0
> 0 0 0 0 0 0 0
>
> B =
>
> 2 3 4
> 3 1 2
> 2 3 1
>
> I want
>
> C =
> 0 0 0 0 0 0 0
> 0 2 3 4 0 0 0
> 0 3 1 2 0 0 0
> 0 2 3 1 0 0 0
> 0 0 0 0 0 0 0
>
> thanks

Question 1:
B = A;
B(ismember(B,zeros(1,size(B,2)),'rows'),:) = [];
B(:,ismember(B',zeros(1,size(B,1)),'rows')) = [];
%%%%%%%%%%%%%%%%%%%%
B =
     2 3 4
     3 1 2
     2 3 1


Question 2:
C = zeros(size(A));
C(A>0) = B;
%%%%%%%%%%%%%%%%%%%%%%
C =
     0 0 0 0 0 0 0
     0 2 3 4 0 0 0
     0 3 1 2 0 0 0
     0 2 3 1 0 0 0
     0 0 0 0 0 0 0


-Nathan

Subject: select a submatrix with values different from 0

From: Matt Fig

Date: 30 Oct, 2009 21:29:03

Message: 4 of 5

B = reshape(A(A~=0),[],nnz(sum(A)))

C as Nathan directs.

Subject: select a submatrix with values different from 0

From: Bruno Luong

Date: 30 Oct, 2009 21:38:02

Message: 5 of 5

A(any(A,2),any(A,1))

% Bruno

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us