Thread Subject: If there is a NaN anywhere in a 4d matrix, I want to remove the column. How?

Subject: If there is a NaN anywhere in a 4d matrix, I want to remove the column. How?

From: John Trimper

Date: 5 Feb, 2012 03:58:20

Message: 1 of 4

I have a 4d matrix that has many columns that are NaNs. I want to remove all of these columns.

So simplified, it's something like this...

y(:,:,1,1) =
     1 2 NaN 4
     1 2 NaN 4
     1 2 NaN 4
     1 2 NaN 4

y(:,:,2,1) =
     1 2 3 4
     1 2 3 4
     1 2 3 4
     1 2 3 4

y(:,:,1,2) =
     1 NaN 3 4
     1 NaN 3 4
     1 NaN 3 4
     1 NaN 3 4

y(:,:,2,2) =
     1 2 3 4
     1 2 3 4
     1 2 3 4
     1 2 3 4


I tried...
>> for a=1:4; for b=1:2; for c=1:2; if isnan(y(1,a, b, c)); y(:,a,:,:)=[]; end; end; end; end

but I wind up with...

??? Index exceeds matrix dimensions.
 >> y

y(:,:,1,1) =
     1 NaN 4
     1 NaN 4
     1 NaN 4
     1 NaN 4

y(:,:,2,1) =
     1 3 4
     1 3 4
     1 3 4
     1 3 4

y(:,:,1,2) =
     1 3 4
     1 3 4
     1 3 4
     1 3 4

y(:,:,2,2) =
     1 3 4
     1 3 4
     1 3 4
     1 3 4


What I really want is...

y(:,:,1,1) =
     1 4
     1 4
     1 4
     1 4

y(:,:,2,1) =
     1 4
     1 4
     1 4
     1 4

y(:,:,1,2) =
     1 4
     1 4
     1 4
     1 4

y(:,:,2,2) =
     1 4
     1 4
     1 4
     1 4


Please help me with this issue?

Thanks so much!
John

Subject: If there is a NaN anywhere in a 4d matrix, I want to remove the column. How?

From: Bruno Luong

Date: 5 Feb, 2012 09:41:11

Message: 2 of 4

 y(:,any(any(any(isnan(y),1),3),4),:,:) = []

% Bruno

Subject: If there is a NaN anywhere in a 4d matrix, I want to remove the column. How?

From: John Trimper

Date: 5 Feb, 2012 15:48:10

Message: 3 of 4

Wonderful.

Thanks Bruno!

Best,
John

Subject: If there is a NaN anywhere in a 4d matrix, I want to remove the column. How?

From: John Trimper

Date: 5 Feb, 2012 16:03:10

Message: 4 of 4

I spoke a little too soon.

That certainly does the trick with what I gave you, but I need it to work for a variable number of columns and rows. In my data, I have anywhere from 10-24 columns and 60-150 rows.

I'm picking apart the command you gave me now to see if I can get it, but no success yet.

Thanks for the help,
John



"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jglirn$18l$1@newscl01ah.mathworks.com>...
> y(:,any(any(any(isnan(y),1),3),4),:,:) = []
>
> % Bruno

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
nan John Trimper 4 Feb, 2012 22:59:30
4d matrix John Trimper 4 Feb, 2012 22:59:30
delete columns John Trimper 4 Feb, 2012 22:59:30
rssFeed for this Thread

Contact us at files@mathworks.com