How to delete the last n elements of an array?

176 views (last 30 days)
Hi, I am trying to delete the bottom n rows of an N X 2 array. I don't know how to do it. Would you please help?
For example, suppose that I have the matrix M=[3 6 3 7 9 11 5 34; 1 4 6 8 98 3 4 45]. I would like to delete the, say,the bottom 2 rows to get the new M=[3 6 3 7 9 11; 1 4 6 8 98 3]
How can I do this?
Please help.
Thank you.

Accepted Answer

Miroslav Balda
Miroslav Balda on 11 Mar 2013
It is simple
M = M(1:end-2,:)
  3 Comments
Image Analyst
Image Analyst on 19 Aug 2017
"1:end-2" specified rows from 1 through 2 before the last row. Now you need to specify the columns since M is a 2-D matrix with both rows and columns. The comma separates the rows specifier from the column specifier. The column specifier is ":" which means "all". So it means rows 1 through the end-2 and all columns of those rows.
franco otaola
franco otaola on 15 Apr 2020
Edited: franco otaola on 15 Apr 2020
hello,
i knew how to do it, but to do this, you have to have M decleared before, i find myself with this problem all the time in matlab and obviously it is that i am missing something:
for example i am calculating a convolution of signals (it is just an example of where i find myself with this issue),
S1=[1:1:100]'; %for S1 and h i took simpler examples of arrays to be more clear
h=[1:1:100]';
O1=conv(S1,h); %my problem is that i want to cut the O1 to the same size as S1
% and h, as the rest of the convolution is going to be 0
%{
so i would like to do something like this
O1=conv(S1,h)(1:100);
i know i can do:
O1=conv(S1,h);
O1=O1(1:100); but is it possible to do it in one line?
%}
best regards, franco!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!