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:
conditional column delete (3D matrix)

Subject: conditional column delete (3D matrix)

From: Thomas

Date: 11 Oct, 2012 14:54:09

Message: 1 of 3

I know how to accomplish this with a 2-D matrix, but I am having trouble with the syntax to apply to a 3-D matrix

2-D example: Let's say A is a 10x5 matrix.
If any column of A meets a criterion, I want to delete the whole column. If my criterion is any element greater than 10, I can do this:
A = rand(10,5)*10; A(3,2) = 11; A(5,4) = 12; % create matrix
A(:,max(A)>10) = []; % delete the two columns containing number > 10
I'm left with a 10x3 matrix comprising the original columns 1, 3 and 5.

But, what if it's a 10x5x2 matrix. Same criterion, but now, when criterion is met, I want to delete columns across both levels of the 3rd dimension. (That is, my goal is a 10x3x2).
Note, the criterion would apply to either level of the 3rd dimension. So...
B = rand(10,5,2)*10; B(3,2,1) = 11; B(5,4,2) = 12;
Columns 2 and 4 meet the criterion regardless of whether the location of the criterion-meeting number is in (:,:,1) or (:,:,2).
The logic from the 2-D examples above led me to think that something like this should work:
B(:,max(B)>10,:) = [];
But no joy. I get 'Matrix index is out of range for deletion.' I've tried several other variants and I can't quite get it. Suggestions? Thanks,
-Tom

Subject: conditional column delete (3D matrix)

From: Roger Stafford

Date: 11 Oct, 2012 16:56:07

Message: 2 of 3

"Thomas" wrote in message <k56mih$rv3$1@newscl01ah.mathworks.com>...
> I know how to accomplish this with a 2-D matrix, but I am having trouble with the syntax to apply to a 3-D matrix
>
> 2-D example: Let's say A is a 10x5 matrix.
> If any column of A meets a criterion, I want to delete the whole column. If my criterion is any element greater than 10, I can do this:
> A = rand(10,5)*10; A(3,2) = 11; A(5,4) = 12; % create matrix
> A(:,max(A)>10) = []; % delete the two columns containing number > 10
> I'm left with a 10x3 matrix comprising the original columns 1, 3 and 5.
>
> But, what if it's a 10x5x2 matrix. Same criterion, but now, when criterion is met, I want to delete columns across both levels of the 3rd dimension. (That is, my goal is a 10x3x2).
> Note, the criterion would apply to either level of the 3rd dimension. So...
> B = rand(10,5,2)*10; B(3,2,1) = 11; B(5,4,2) = 12;
> Columns 2 and 4 meet the criterion regardless of whether the location of the criterion-meeting number is in (:,:,1) or (:,:,2).
> The logic from the 2-D examples above led me to think that something like this should work:
> B(:,max(B)>10,:) = [];
> But no joy. I get 'Matrix index is out of range for deletion.' I've tried several other variants and I can't quite get it. Suggestions? Thanks,
> -Tom
- - - - - - - - - - - -
  How about using the 'any' function?

 B(:,any(any(B>10,2),3),:) = [];

Or perhaps it should be:

 B(:,any(any(B>10,2),2),:) = [];

if the inner 'any' result collapses to a true two-dimensional array. I haven't tried this out.

Roger Stafford

Subject: conditional column delete (3D matrix)

From: Thomas

Date: 11 Oct, 2012 20:23:08

Message: 3 of 3

"Roger Stafford" wrote in message <k56tn7$qf1$1@newscl01ah.mathworks.com>...
> How about using the 'any' function?
>
> B(:,any(any(B>10,2),3),:) = [];
>
> Or perhaps it should be:
>
> B(:,any(any(B>10,2),2),:) = [];
>
> if the inner 'any' result collapses to a true two-dimensional array. I haven't tried this out.
>
> Roger Stafford

Thanks, Roger. That got me pointed in the right direction. The correct usage is:
B(:,any(any(B>10,1),3),:) = [];
or, to remove an unneeded default arg:
B(:,any(any(B>10),3),:) = [];
-t

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