I have an nx1
vector in Matlab. I want to delete rows starting from a specific index
. For example, if n is 100
and the index is 60
then all rows from 60 till 100 will be removed. I've found this REMOVEROWS
but I don't know how to make it do this.
Asked
Active
Viewed 94 times
0

Tak
- 3,536
- 11
- 51
- 93
-
2This is pretty basic Matlab, but see [this question and answer](http://stackoverflow.com/questions/19633192/memory-efficient-way-to-truncate-large-array-in-matlab/19648283#19648283) for discussion on the best way (at the time at least) to do this: `a=rand(100,1);` `a=a(1:59);` – horchler May 26 '14 at 03:38
-
thanks! could you make this as an answer to mark it as solved? – Tak May 26 '14 at 03:42
2 Answers
2
The removerows
function is probably overkill for a vector, but here is how it can be used along with the usual linear indexing method:
n = 100;
index = 60;
a = rand(n,1); % An n-by-1 column vector
b1 = a(1:index-1)
b2 = removerows(a,'ind',index:n) % Or removerows(a,'ind',index:size(a,1))
Note that the removerows
function is in the Neural Network toolbox and thus not part of core Matlab.

horchler
- 18,384
- 4
- 37
- 73
1
This should the trick:
your_vector(index:end) = [];
If you want to remove from the end that you can do:
your_vector(end-index+1:end) = [];

Marcin
- 215,873
- 14
- 235
- 294
-
Unfortunately, this may not be the best way to do this if one cares about performance: http://stackoverflow.com/a/19648283/2278029 – horchler May 26 '14 at 04:02
-
-
1