MATLAB Answers

## delete rows based on whether the day is a wednesday or not

Asked by Locks

### Locks (view profile)

on 4 May 2013
Accepted Answer by Azzi Abdelmalek

### Azzi Abdelmalek (view profile)

Hi,
I have a dataset with 18 columns and thousands of rows. In column 6 is always the date of the respective row. I would now like to check for which rows column 6 (dates) is a wednesday.
I already have the following code:
dataset=zeros(size(data18,1),19);
dataset(:,1:18)=data18;
numberEl=length(dataset(:,1));
numberID=[1:numberEl]';
dataset(:,19)=numberID;
d1=dataset(:,6);
dates=datestr(d1,'ddd-mm-yyyy');
M=[cellstr(dates) num2cell((1:numberEl)')];
M=M(ismember(dates(:,1:3),'Wed','rows'),:);
nums = [M{:,2}];
rowsToKeep=nums';
rowsToDeleteNotWednesday=dataset(:,19)~rowsToKeep;
dataset(rowsToDeleteNotWednesday, :) = [];
The last two lines of code is not working. In column 19, I have all numbers from one till the end of the vector and I though that there could be a way that everytime that number is not equal to one of the numbers in vector rowsToKeep, the entire row will be deleted.

#### 0 Comments

Sign in to comment.

## 1 Answer

Answer by Azzi Abdelmalek

### Azzi Abdelmalek (view profile)

on 4 May 2013
Accepted Answer

You can use
idx=find(ismember(dates(:,1:3),'Wed','rows'));

Locks

### Locks (view profile)

on 4 May 2013
that is the same is the rowsToKeep vector isn't it?
the point where I am struggling is how I can create a matrix with all the columns, based on the matrix dataset, but with only those rows which are in idx or rowsToKeep
Azzi Abdelmalek

### Azzi Abdelmalek (view profile)

on 4 May 2013
dataset=zeros(size(data18,1),19);
dataset(:,1:18)=data18;
numberEl=length(dataset(:,1));
numberID=[1:numberEl]';
dataset(:,19)=numberID;
d1=dataset(:,6);
dates=datestr(d1,'ddd-mm-yyyy');
rowsToKeep=find(ismember(dates(:,1:3),'Wed','rows'));
final_dataset=dataset(rowsToKeep,:)
Locks

### Locks (view profile)

on 4 May 2013
perfect, thanks!

Sign in to comment.