delete certain elements in a matrix

Is there any way to delete certain rows(with a pattern) in a matrix
like to delete rows 1,6,11,21,26,..... using a loop
Thanks.

 Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 14 Mär. 2013
Bearbeitet: Azzi Abdelmalek am 14 Mär. 2013

1 Stimme

A=rand(40,4)
v=[1 6 11 21 26]
w=sort(v);
for k=0:numel(v)-1
A(w(k+1)-k,:)=[]
end

7 Kommentare

dav
dav am 14 Mär. 2013
thanks I want to delete all the rows. not just the above 4.
But all the rows in the pattern
1,6,11,16,21,26,31,..............101
Azzi Abdelmalek
Azzi Abdelmalek am 14 Mär. 2013
Bearbeitet: Azzi Abdelmalek am 14 Mär. 2013
A=rand(110,4)
w=1:5:101
for k=1:numel(v)
A(w(k)-k+1,:)=[]
end
dav
dav am 14 Mär. 2013
could you please explain this ?
dav
dav am 14 Mär. 2013
I made a mistake in my question.
I want to delete all the OTHER ROWS EXCEPT THE ROWS 1,6,11,16,......
Azzi Abdelmalek
Azzi Abdelmalek am 14 Mär. 2013
Bearbeitet: Azzi Abdelmalek am 14 Mär. 2013
% for k=1, w(1)=1; A(w(k)-k+1,:)=A(1+1-1)=A(1,:)
% for k=2, w(2)=6; A(w(k)-k+1,:)=A(6+2-1)=A(6,:)
% and so on, all those rows will be deleted by A(w(k)-k+1,:)=[]
Jan
Jan am 14 Mär. 2013
@dav: Simply set a breakpoint in the code an let it run. In the first iteration the first row is deleted. In the 2nd iteration, you should not delete the 6th row, because in the first iteration the 6th row became the 5th one. Therefore (k-1) is subtracted from the index in the k.th iteration.
Nevertheless, a loop is an inefficient approach and suffers from the old Schlemiel the painter problem.
dav
dav am 14 Mär. 2013
Thanks a lot

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Sven
Sven am 14 Mär. 2013
Bearbeitet: Sven am 14 Mär. 2013

3 Stimmen

In a loop? Not really.
All at once? Absolutely.
DATA = rand(50,5);
rowsToDelete = [1,6,11,21,26];
DATA(rowsToDelete,:) = [];
If you mean that your rows to delete has the pattern of rows 1,6,11,16, etc, then just do this:
numRows = size(DATA,1);
rowsToDelete = [1:10:numRows 6:10:numRows];
DATA(rowsToDelete,:) = [];

3 Kommentare

dav
dav am 14 Mär. 2013
thanks, but I made a mistake in my question.
I want to delete all the OTHER ROWS EXCEPT THE ROWS 1,6,11,16,......
Could you please adjust your code to do this?
Thanks
Jan
Jan am 14 Mär. 2013
This is much faster than a loop for large arrays. Shrinking arrays in a loop has the same drawback as letting them grow: In each iteration the new array must be allocated and the contents of the old one is copied. This is very expensive.
dav
dav am 14 Mär. 2013
Thanks a lot

Melden Sie sich an, um zu kommentieren.

Jan
Jan am 14 Mär. 2013

2 Stimmen

Alternative to Sven's approach:
Data = rand(50,5);
index = false(1, 50);
index(1:10:end) = true;
index(6:10:end) = true;
Data(index, :) = [];

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by