tags:

views:

43

answers:

3

I have an array, let's say

A =

     1     2     3
     4     5     6
     7     8     9
     2     3     4
     3     4     5
     1     6     8

I took a point from 3rd column, let's say p=9.

Now I want a new array that gives me all values after 9 and discards values before 9. For example:

ans =

     7     8     9
     2     3     4
     3     4     5
     1     6     8

How do I do it?

+2  A: 

Here's one way:

>> p = 9;
>> startrow = find(A(:, 3) == p, 1); % first row where 3rd column entry is p
>> A1 = A(startrow:end, :)

A1 =

     7     8     9
     2     3     4
     3     4     5
     1     6     8

Edit:

With multiple entries of p in the last column, you can vary how find is used. For example, if you want to start at the last p instead of the first, you could run

>> startrow = find(A(:, 3) == p, 1, 'last'); % last row where 3rd column entry is p

If you want the second from first, you'd need a little more work:

>> startrows = find(A(:, 3) == p, 2); % first two rows where 3rd column entry is p
>> startrow = startrows(end); % Just the second row where 3rd column entry is p

Also, you probably need error checking if there is the possibility that p never appears in the array, or doesn't appear at least two times (for the last example).

SCFrench
if the matrix is like this A=[1 2 3;4 5 6;7 8 9;2 3 9;3 4 9;1 6 8] i-e third colum contains 9 three times and i want to select 5th row that has 9 in it which is 3 4 9.......how to select it?
chee
You can just output find result to a vector `startrow=find(A(:,3)==p);`, then use any index you want: `A1=A(startrow(2):end,:);`
yuk
if i want to discard values after point i-e p=9 and want to retain values before this point....than what to do?
chee
A1 = A(1:startrow, :);
SCFrench
+1  A: 

If you know what column the point is from you can simply do

B = A(n:end,:)

where n is the column number.

Ghaul
A: 

Another option is to delete the rows directly from the original matrix:

A(1:startrow-1,:)=[];
yuk

related questions