I need to loop through each 2-D array in a 3-d matrix and remove all rows in each 2-3 Array that has 0 in column 2.

3 views (last 30 days)
I have a data set that is 360 x 5 x 20 (20 years of annual measurements). Each of the 360 by 5 annual sets has many zeros in column 2 randomly distributed across rows. All rows of all 20 sets containing a zero in any row of column 2 need to be deleted. If I select one set at a time the following works: Testindex = TEST(:,2); idx = Testindex ~=0; TESTOUT = TEST(idx,:);, where TEST is one of the 360 by 5 data sets. I cannot write a code to extend this to loop through each of the 20 sets and perform the removal operation. I have tried for i = 1:20 Testindex2(:,i) = TEST2(:,2,i); idx2(:,i) = Testindex2(:,i) ~=0; TESTOUT2(:,:,i) = TEST2(idx2(:,i),:); end.
How can I do this?
NIckG

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 1 Sep 2013
Edited: Azzi Abdelmalek on 1 Sep 2013
Edit
A=randi([0 9],360,5,20); %Example
%-----------------------------------
[n,m,p]=size(A);
B=reshape(A,n,[]);
B(any(B(:,2:m:end)==0,2),:)=[];
out=reshape(B,[],m,p)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!