Got Questions? Get Answers.
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:
Remove all columns with only zeros from a three dimensional matrix

Subject: Remove all columns with only zeros from a three dimensional matrix

From: Hermano Cappa

Date: 4 Apr, 2012 12:11:12

Message: 1 of 2

Hi,

I'm looking for a fast way (without if-then-else routine) to remove all the columns which only contains zeros from a three dimensional matrix matrix.

For example:

A = cat( 3, [0 2 2 2 0; 0 2 2 2 0], [0 2 2 2 0; 0 2 2 2 0], [0 2 2 2 0; 0 2 2 2 0])

will result in:

A(:,:,1) =

     2 2 2
     2 2 2

A(:,:,2) =

     2 2 2
     2 2 2

A(:,:,3) =

     2 2 2
     2 2 2

Subject: Remove all columns with only zeros from a three dimensional matrix

From: Matt J

Date: 4 Apr, 2012 13:07:11

Message: 2 of 2

"Hermano Cappa" wrote in message <jlhdp0$9ii$1@newscl01ah.mathworks.com>...
> Hi,
>
> I'm looking for a fast way (without if-then-else routine) to remove all the columns which only contains zeros from a three dimensional matrix matrix.
============

In general, that's impossible unless the same columns contain zeros in every slice
A(:,:,i). But if you're saying that will always be true, then

[m,n,p]=size(A);
A(:,~any(A,1))=[];
A=reshape(A,m,[],p)

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